Skip to content

Commit 27c4688

Browse files
TempFile changes to support JRUBY
closes ruby-git#72
1 parent cc0e3bb commit 27c4688

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

lib/git/base.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,17 @@ def with_index(new_index) # :yields: new_index
416416
end
417417

418418
def with_temp_index &blk
419-
tempfile = Tempfile.new('temp-index')
420-
temp_path = tempfile.path
421-
tempfile.unlink
419+
# Workaround for JRUBY, since they handle the TempFile path different.
420+
# MUST be improved to be safer and OS independent.
421+
if RUBY_PLATFORM == 'java'
422+
temp_path = "/tmp/temp-index-#{(0...15).map{ ('a'..'z').to_a[rand(26)] }.join}"
423+
else
424+
tempfile = Tempfile.new('temp-index')
425+
temp_path = tempfile.path
426+
tempfile.close
427+
tempfile.unlink
428+
end
429+
422430
with_index(temp_path, &blk)
423431
end
424432

@@ -466,6 +474,7 @@ def with_working(work_dir) # :yields: the Git::WorkingDirectory
466474
def with_temp_working &blk
467475
tempfile = Tempfile.new("temp-workdir")
468476
temp_dir = tempfile.path
477+
tempfile.close
469478
tempfile.unlink
470479
Dir.mkdir(temp_dir, 0700)
471480
with_working(temp_dir, &blk)

lib/git/path.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
module Git
2-
class Path
2+
3+
class Path
34

45
attr_accessor :path
56

6-
def initialize(path, check_path = true)
7-
if !check_path || File.exists?(path)
8-
@path = File.expand_path(path)
9-
else
7+
def initialize(path, check_path=true)
8+
if check_path && !File.exists?(path)
109
raise ArgumentError, 'path does not exist', [File.expand_path(path)]
1110
end
11+
12+
@path = File.expand_path(path)
1213
end
1314

1415
def readable?
@@ -24,4 +25,5 @@ def to_s
2425
end
2526

2627
end
28+
2729
end

tests/units/test_tree_ops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def test_read_tree
9090

9191
tmp = Tempfile.new('tesxt')
9292
tmppath = tmp.path
93+
tmp.close
9394
tmp.unlink
9495

9596
g.with_index(tmppath) do

0 commit comments

Comments
 (0)