Skip to content

Commit 836ff95

Browse files
neoeinsteingitster
authored andcommitted
Git.pm: Use File::Temp->tempfile instead of ->new
Perl 5.8.0 ships with File::Temp 0.13, which does not have the new() interface introduced in 0.14, as pointed out by Tom G. Christensen. This modifies Git.pm to use the more established tempfile() interface and updates 'git svn' to match. Signed-off-by: Marcus Griep <[email protected]> Acked-by: Eric Wong <[email protected]> Tested-by: Tom G. Christensen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1e36868 commit 836ff95

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

git-svn.perl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3284,7 +3284,7 @@ sub close_file {
32843284
my $out = syswrite($tmp_fh, $str, $res);
32853285
defined($out) && $out == $res
32863286
or croak("write ",
3287-
$tmp_fh->filename,
3287+
Git::temp_path($tmp_fh),
32883288
": $!\n");
32893289
}
32903290
defined $res or croak $!;
@@ -3295,7 +3295,7 @@ sub close_file {
32953295
}
32963296

32973297
$hash = $::_repository->hash_and_insert_object(
3298-
$fh->filename);
3298+
Git::temp_path($fh));
32993299
$hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
33003300

33013301
Git::temp_release($fb->{base}, 1);

perl/Git.pm

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ require Exporter;
5858
command_bidi_pipe command_close_bidi_pipe
5959
version exec_path hash_object git_cmd_try
6060
remote_refs
61-
temp_acquire temp_release temp_reset);
61+
temp_acquire temp_release temp_reset temp_path);
6262

6363

6464
=head1 DESCRIPTION
@@ -937,7 +937,7 @@ sub _close_cat_blob {
937937

938938
{ # %TEMP_* Lexical Context
939939

940-
my (%TEMP_LOCKS, %TEMP_FILES);
940+
my (%TEMP_FILEMAP, %TEMP_FILES);
941941

942942
=item temp_acquire ( NAME )
943943
@@ -965,7 +965,7 @@ sub temp_acquire {
965965

966966
my $temp_fd = _temp_cache($name);
967967

968-
$TEMP_LOCKS{$temp_fd} = 1;
968+
$TEMP_FILES{$temp_fd}{locked} = 1;
969969
$temp_fd;
970970
}
971971

@@ -991,16 +991,16 @@ the same string.
991991
sub temp_release {
992992
my ($self, $temp_fd, $trunc) = _maybe_self(@_);
993993

994-
if (ref($temp_fd) ne 'File::Temp') {
994+
if (exists $TEMP_FILEMAP{$temp_fd}) {
995995
$temp_fd = $TEMP_FILES{$temp_fd};
996996
}
997-
unless ($TEMP_LOCKS{$temp_fd}) {
997+
unless ($TEMP_FILES{$temp_fd}{locked}) {
998998
carp "Attempt to release temp file '",
999999
$temp_fd, "' that has not been locked";
10001000
}
10011001
temp_reset($temp_fd) if $trunc and $temp_fd->opened;
10021002

1003-
$TEMP_LOCKS{$temp_fd} = 0;
1003+
$TEMP_FILES{$temp_fd}{locked} = 0;
10041004
undef;
10051005
}
10061006

@@ -1009,9 +1009,9 @@ sub _temp_cache {
10091009

10101010
_verify_require();
10111011

1012-
my $temp_fd = \$TEMP_FILES{$name};
1012+
my $temp_fd = \$TEMP_FILEMAP{$name};
10131013
if (defined $$temp_fd and $$temp_fd->opened) {
1014-
if ($TEMP_LOCKS{$$temp_fd}) {
1014+
if ($TEMP_FILES{$$temp_fd}{locked}) {
10151015
throw Error::Simple("Temp file with moniker '",
10161016
$name, "' already in use");
10171017
}
@@ -1021,12 +1021,13 @@ sub _temp_cache {
10211021
carp "Temp file '", $name,
10221022
"' was closed. Opening replacement.";
10231023
}
1024-
$$temp_fd = File::Temp->new(
1025-
TEMPLATE => 'Git_XXXXXX',
1026-
DIR => File::Spec->tmpdir
1024+
my $fname;
1025+
($$temp_fd, $fname) = File::Temp->tempfile(
1026+
'Git_XXXXXX', UNLINK => 1
10271027
) or throw Error::Simple("couldn't open new temp file");
10281028
$$temp_fd->autoflush;
10291029
binmode $$temp_fd;
1030+
$TEMP_FILES{$$temp_fd}{fname} = $fname;
10301031
}
10311032
$$temp_fd;
10321033
}
@@ -1053,8 +1054,25 @@ sub temp_reset {
10531054
or throw Error::Simple("expected file position to be reset");
10541055
}
10551056

1057+
=item temp_path ( NAME )
1058+
1059+
=item temp_path ( FILEHANDLE )
1060+
1061+
Returns the filename associated with the given tempfile.
1062+
1063+
=cut
1064+
1065+
sub temp_path {
1066+
my ($self, $temp_fd) = _maybe_self(@_);
1067+
1068+
if (exists $TEMP_FILEMAP{$temp_fd}) {
1069+
$temp_fd = $TEMP_FILEMAP{$temp_fd};
1070+
}
1071+
$TEMP_FILES{$temp_fd}{fname};
1072+
}
1073+
10561074
sub END {
1057-
unlink values %TEMP_FILES if %TEMP_FILES;
1075+
unlink values %TEMP_FILEMAP if %TEMP_FILEMAP;
10581076
}
10591077

10601078
} # %TEMP_* Lexical Context

0 commit comments

Comments
 (0)