Skip to content

Commit

Permalink
Git.pm: Use File::Temp->tempfile instead of ->new
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
neoeinstein authored and gitster committed Sep 10, 2008
1 parent 1e36868 commit 836ff95
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions git-svn.perl
Original file line number Diff line number Diff line change
Expand Up @@ -3284,7 +3284,7 @@ sub close_file {
my $out = syswrite($tmp_fh, $str, $res);
defined($out) && $out == $res
or croak("write ",
$tmp_fh->filename,
Git::temp_path($tmp_fh),
": $!\n");
}
defined $res or croak $!;
Expand All @@ -3295,7 +3295,7 @@ sub close_file {
}

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

Git::temp_release($fb->{base}, 1);
Expand Down
42 changes: 30 additions & 12 deletions perl/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ require Exporter;
command_bidi_pipe command_close_bidi_pipe
version exec_path hash_object git_cmd_try
remote_refs
temp_acquire temp_release temp_reset);
temp_acquire temp_release temp_reset temp_path);


=head1 DESCRIPTION
Expand Down Expand Up @@ -937,7 +937,7 @@ sub _close_cat_blob {

{ # %TEMP_* Lexical Context

my (%TEMP_LOCKS, %TEMP_FILES);
my (%TEMP_FILEMAP, %TEMP_FILES);

=item temp_acquire ( NAME )
Expand Down Expand Up @@ -965,7 +965,7 @@ sub temp_acquire {

my $temp_fd = _temp_cache($name);

$TEMP_LOCKS{$temp_fd} = 1;
$TEMP_FILES{$temp_fd}{locked} = 1;
$temp_fd;
}

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

if (ref($temp_fd) ne 'File::Temp') {
if (exists $TEMP_FILEMAP{$temp_fd}) {
$temp_fd = $TEMP_FILES{$temp_fd};
}
unless ($TEMP_LOCKS{$temp_fd}) {
unless ($TEMP_FILES{$temp_fd}{locked}) {
carp "Attempt to release temp file '",
$temp_fd, "' that has not been locked";
}
temp_reset($temp_fd) if $trunc and $temp_fd->opened;

$TEMP_LOCKS{$temp_fd} = 0;
$TEMP_FILES{$temp_fd}{locked} = 0;
undef;
}

Expand All @@ -1009,9 +1009,9 @@ sub _temp_cache {

_verify_require();

my $temp_fd = \$TEMP_FILES{$name};
my $temp_fd = \$TEMP_FILEMAP{$name};
if (defined $$temp_fd and $$temp_fd->opened) {
if ($TEMP_LOCKS{$$temp_fd}) {
if ($TEMP_FILES{$$temp_fd}{locked}) {
throw Error::Simple("Temp file with moniker '",
$name, "' already in use");
}
Expand All @@ -1021,12 +1021,13 @@ sub _temp_cache {
carp "Temp file '", $name,
"' was closed. Opening replacement.";
}
$$temp_fd = File::Temp->new(
TEMPLATE => 'Git_XXXXXX',
DIR => File::Spec->tmpdir
my $fname;
($$temp_fd, $fname) = File::Temp->tempfile(
'Git_XXXXXX', UNLINK => 1
) or throw Error::Simple("couldn't open new temp file");
$$temp_fd->autoflush;
binmode $$temp_fd;
$TEMP_FILES{$$temp_fd}{fname} = $fname;
}
$$temp_fd;
}
Expand All @@ -1053,8 +1054,25 @@ sub temp_reset {
or throw Error::Simple("expected file position to be reset");
}

=item temp_path ( NAME )
=item temp_path ( FILEHANDLE )
Returns the filename associated with the given tempfile.
=cut

sub temp_path {
my ($self, $temp_fd) = _maybe_self(@_);

if (exists $TEMP_FILEMAP{$temp_fd}) {
$temp_fd = $TEMP_FILEMAP{$temp_fd};
}
$TEMP_FILES{$temp_fd}{fname};
}

sub END {
unlink values %TEMP_FILES if %TEMP_FILES;
unlink values %TEMP_FILEMAP if %TEMP_FILEMAP;
}

} # %TEMP_* Lexical Context
Expand Down

0 comments on commit 836ff95

Please sign in to comment.