Skip to content

Commit 4e63dcc

Browse files
mackylegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent edca415 commit 4e63dcc

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

perl/Git.pm

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ require Exporter;
6161
remote_refs prompt
6262
get_tz_offset
6363
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);
6565

6666

6767
=head1 DESCRIPTION
@@ -1206,6 +1206,35 @@ sub temp_acquire {
12061206
$temp_fd;
12071207
}
12081208

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+
12091238
=item temp_release ( NAME )
12101239
12111240
=item temp_release ( FILEHANDLE )
@@ -1248,7 +1277,7 @@ sub _temp_cache {
12481277

12491278
my $temp_fd = \$TEMP_FILEMAP{$name};
12501279
if (defined $$temp_fd and $$temp_fd->opened) {
1251-
if ($TEMP_FILES{$$temp_fd}{locked}) {
1280+
if (temp_is_locked($name)) {
12521281
throw Error::Simple("Temp file with moniker '" .
12531282
$name . "' already in use");
12541283
}

0 commit comments

Comments
 (0)