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

Make 'cvs ci' lockless in git-cvsserver by using git-update-ref

This makes "ci" codepath lockless by following the usual "remember the tip, do your thing, then compare and swap at the end" update pattern using update-ref. Incidentally, by updating the code that reads where the tip of the head is to use show-ref, it makes it safe to use in a repository whose refs are pack-pruned. I noticed that other parts of the program are not yet pack-refs safe, but tried to keep the changes to the minimum. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 20, 2007 at 21:54 UTC ada5ef3b48be7ded62af1f9b7fdb71db3024ac71
1 file changed +16 -27
git-cvsserver.perl
+16 -27
@@ -1031,36 +1031,35 @@ sub req_ci
1031 exit;
1032 }
1033
1034 - my $lockfile = "$state->{CVSROOT}/refs/heads/$state->{module}.lock";
1035 - unless ( sysopen(LOCKFILE,$lockfile,O_EXCL|O_CREAT|O_WRONLY) )
1036 - {
1037 - $log->warn("lockfile '$lockfile' already exists, please try again");
1038 - print "error 1 Lock file '$lockfile' already exists, please try again\n";
1039 - exit;
1040 - }
1041 -
1034 # Grab a handle to the SQLite db and do any necessary updates
1035 my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
1036 $updater->update();
1037
1038 my $tmpdir = tempdir ( DIR => $TEMP_DIR );
1039 my ( undef, $file_index ) = tempfile ( DIR => $TEMP_DIR, OPEN => 0 );
1048 - $log->info("Lock successful, basing commit on '$tmpdir', index file is '$file_index'");
1040 + $log->info("Lockless commit start, basing commit on '$tmpdir', index file is '$file_index'");
1041
1042 $ENV{GIT_DIR} = $state->{CVSROOT} . "/";
1043 $ENV{GIT_INDEX_FILE} = $file_index;
1044
1045 + # Remember where the head was at the beginning.
1046 + my $parenthash = `git show-ref -s refs/heads/$state->{module}`;
1047 + chomp $parenthash;
1048 + if ($parenthash !~ /^[0-9a-f]{40}$/) {
1049 + print "error 1 pserver cannot find the current HEAD of module";
1050 + exit;
1051 + }
1052 +
1053 chdir $tmpdir;
1054
1055 # populate the temporary index based
1056 - system("git-read-tree", $state->{module});
1056 + system("git-read-tree", $parenthash);
1057 unless ($? == 0)
1058 {
1059 die "Error running git-read-tree $state->{module} $file_index $!";
1060 }
1061 $log->info("Created index '$file_index' with for head $state->{module} - exit status $?");
1062
1063 -
1063 my @committedfiles = ();
1064
1065 # foreach file specified on the command line ...
@@ -1095,8 +1094,6 @@ sub req_ci
1094 {
1095 # fail everything if an up to date check fails
1096 print "error 1 Up to date check failed for $filename\n";
1098 - close LOCKFILE;
1099 - unlink($lockfile);
1097 chdir "/";
1098 exit;
1099 }
@@ -1139,16 +1136,12 @@ sub req_ci
1136 {
1137 print "E No files to commit\n";
1138 print "ok\n";
1142 - close LOCKFILE;
1143 - unlink($lockfile);
1139 chdir "/";
1140 return;
1141 }
1142
1143 my $treehash = `git-write-tree`;
1149 - my $parenthash = `cat $ENV{GIT_DIR}refs/heads/$state->{module}`;
1144 chomp $treehash;
1151 - chomp $parenthash;
1145
1146 $log->debug("Treehash : $treehash, Parenthash : $parenthash");
1147
@@ -1165,8 +1158,6 @@ sub req_ci
1158 {
1159 $log->warn("Commit failed (Invalid commit hash)");
1160 print "error 1 Commit failed (unknown reason)\n";
1168 - close LOCKFILE;
1169 - unlink($lockfile);
1161 chdir "/";
1162 exit;
1163 }
@@ -1179,14 +1170,17 @@ sub req_ci
1170 {
1171 $log->warn("Commit failed (update hook declined to update ref)");
1172 print "error 1 Commit failed (update hook declined)\n";
1182 - close LOCKFILE;
1183 - unlink($lockfile);
1173 chdir "/";
1174 exit;
1175 }
1176 }
1177
1189 - print LOCKFILE $commithash;
1178 + if (system(qw(git update-ref -m), "cvsserver ci",
1179 + "refs/heads/$state->{module}", $commithash, $parenthash)) {
1180 + $log->warn("update-ref for $state->{module} failed.");
1181 + print "error 1 Cannot commit -- update first\n";
1182 + exit;
1183 + }
1184
1185 $updater->update();
1186
@@ -1215,12 +1209,7 @@ sub req_ci
1209 }
1210 }
1211
1218 - close LOCKFILE;
1219 - my $reffile = "$ENV{GIT_DIR}refs/heads/$state->{module}";
1220 - unlink($reffile);
1221 - rename($lockfile, $reffile);
1212 chdir "/";
1223 -
1213 print "ok\n";
1214 }
1215