Git.pm: add new temp_is_locked function

The temp_is_locked function can be used to determine whether or not a given name previously passed to temp_acquire is currently locked. Signed-off-by: Kyle J. McKay <mackyle@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kyle J. McKay committed Jul 6, 2013 at 21:20 UTC 4e63dcc86cc77ec86a8d35ff752603a4b44d44a7
1 file changed +31 -2
perl/Git.pm
+31 -2
@@ -61,7 +61,7 @@ require Exporter;
61 remote_refs prompt
62 get_tz_offset
63 credential credential_read credential_write
64 - temp_acquire temp_release temp_reset temp_path);
64 + temp_acquire temp_is_locked temp_release temp_reset temp_path);
65
66
67 =head1 DESCRIPTION
@@ -1206,6 +1206,35 @@ sub temp_acquire {
1206 $temp_fd;
1207 }
1208
1209 +=item temp_is_locked ( NAME )
1210 +
1211 +Returns true if the internal lock created by a previous C<temp_acquire()>
1212 +call with C<NAME> is still in effect.
1213 +
1214 +When temp_acquire is called on a C<NAME>, it internally locks the temporary
1215 +file mapped to C<NAME>. That lock will not be released until C<temp_release()>
1216 +is called with either the original C<NAME> or the L<File::Handle> that was
1217 +returned from the original call to temp_acquire.
1218 +
1219 +Subsequent attempts to call C<temp_acquire()> with the same C<NAME> will fail
1220 +unless there has been an intervening C<temp_release()> call for that C<NAME>
1221 +(or its corresponding L<File::Handle> that was returned by the original
1222 +C<temp_acquire()> call).
1223 +
1224 +If true is returned by C<temp_is_locked()> for a C<NAME>, an attempt to
1225 +C<temp_acquire()> the same C<NAME> will cause an error unless
1226 +C<temp_release> is first called on that C<NAME> (or its corresponding
1227 +L<File::Handle> that was returned by the original C<temp_acquire()> call).
1228 +
1229 +=cut
1230 +
1231 +sub temp_is_locked {
1232 + my ($self, $name) = _maybe_self(@_);
1233 + my $temp_fd = \$TEMP_FILEMAP{$name};
1234 +
1235 + defined $$temp_fd && $$temp_fd->opened && $TEMP_FILES{$$temp_fd}{locked};
1236 +}
1237 +
1238 =item temp_release ( NAME )
1239
1240 =item temp_release ( FILEHANDLE )
@@ -1248,7 +1277,7 @@ sub _temp_cache {
1277
1278 my $temp_fd = \$TEMP_FILEMAP{$name};
1279 if (defined $$temp_fd and $$temp_fd->opened) {
1251 - if ($TEMP_FILES{$$temp_fd}{locked}) {
1280 + if (temp_is_locked($name)) {
1281 throw Error::Simple("Temp file with moniker '" .
1282 $name . "' already in use");
1283 }