@matheusbsbs / matheus-bagliano / commits / 836ff95df6
Fork 0

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 <marcus@griep.us> Acked-by: Eric Wong <normalperson@yhbt.net> Tested-by: Tom G. Christensen <tgc@statsbiblioteket.dk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Marcus Griep committed Sep 8, 2008 at 12:53 UTC 836ff95df6de9924cf8cf23515e3779a67a6bdec
2 files changed +32 -14
git-svn.perl
+2 -2
@@ -3284,7 +3284,7 @@ sub close_file {
3284 my $out = syswrite($tmp_fh, $str, $res);
3285 defined($out) && $out == $res
3286 or croak("write ",
3287 - $tmp_fh->filename,
3287 + Git::temp_path($tmp_fh),
3288 ": $!\n");
3289 }
3290 defined $res or croak $!;
@@ -3295,7 +3295,7 @@ sub close_file {
3295 }
3296
3297 $hash = $::_repository->hash_and_insert_object(
3298 - $fh->filename);
3298 + Git::temp_path($fh));
3299 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
3300
3301 Git::temp_release($fb->{base}, 1);
perl/Git.pm
+30 -12
@@ -58,7 +58,7 @@ require Exporter;
58 command_bidi_pipe command_close_bidi_pipe
59 version exec_path hash_object git_cmd_try
60 remote_refs
61 - temp_acquire temp_release temp_reset);
61 + temp_acquire temp_release temp_reset temp_path);
62
63
64 =head1 DESCRIPTION
@@ -937,7 +937,7 @@ sub _close_cat_blob {
937
938 { # %TEMP_* Lexical Context
939
940 -my (%TEMP_LOCKS, %TEMP_FILES);
940 +my (%TEMP_FILEMAP, %TEMP_FILES);
941
942 =item temp_acquire ( NAME )
943
@@ -965,7 +965,7 @@ sub temp_acquire {
965
966 my $temp_fd = _temp_cache($name);
967
968 - $TEMP_LOCKS{$temp_fd} = 1;
968 + $TEMP_FILES{$temp_fd}{locked} = 1;
969 $temp_fd;
970 }
971
@@ -991,16 +991,16 @@ the same string.
991 sub temp_release {
992 my ($self, $temp_fd, $trunc) = _maybe_self(@_);
993
994 - if (ref($temp_fd) ne 'File::Temp') {
994 + if (exists $TEMP_FILEMAP{$temp_fd}) {
995 $temp_fd = $TEMP_FILES{$temp_fd};
996 }
997 - unless ($TEMP_LOCKS{$temp_fd}) {
997 + unless ($TEMP_FILES{$temp_fd}{locked}) {
998 carp "Attempt to release temp file '",
999 $temp_fd, "' that has not been locked";
1000 }
1001 temp_reset($temp_fd) if $trunc and $temp_fd->opened;
1002
1003 - $TEMP_LOCKS{$temp_fd} = 0;
1003 + $TEMP_FILES{$temp_fd}{locked} = 0;
1004 undef;
1005 }
1006
@@ -1009,9 +1009,9 @@ sub _temp_cache {
1009
1010 _verify_require();
1011
1012 - my $temp_fd = \$TEMP_FILES{$name};
1012 + my $temp_fd = \$TEMP_FILEMAP{$name};
1013 if (defined $$temp_fd and $$temp_fd->opened) {
1014 - if ($TEMP_LOCKS{$$temp_fd}) {
1014 + if ($TEMP_FILES{$$temp_fd}{locked}) {
1015 throw Error::Simple("Temp file with moniker '",
1016 $name, "' already in use");
1017 }
@@ -1021,12 +1021,13 @@ sub _temp_cache {
1021 carp "Temp file '", $name,
1022 "' was closed. Opening replacement.";
1023 }
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
1027 ) or throw Error::Simple("couldn't open new temp file");
1028 $$temp_fd->autoflush;
1029 binmode $$temp_fd;
1030 + $TEMP_FILES{$$temp_fd}{fname} = $fname;
1031 }
1032 $$temp_fd;
1033 }
@@ -1053,8 +1054,25 @@ sub temp_reset {
1054 or throw Error::Simple("expected file position to be reset");
1055 }
1056
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 +
1074 sub END {
1057 - unlink values %TEMP_FILES if %TEMP_FILES;
1075 + unlink values %TEMP_FILEMAP if %TEMP_FILEMAP;
1076 }
1077
1078 } # %TEMP_* Lexical Context