Skip to content

Commit

Permalink
unset GIT_DIR variable after operation. fix issue minad#5
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Aug 25, 2010
1 parent 7db859c commit 2e882f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
33 changes: 21 additions & 12 deletions lib/gitrb/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,26 +276,27 @@ def put(object)
def method_missing(name, *args, &block)
cmd = name.to_s
if cmd[0..3] == 'git_'
ENV['GIT_DIR'] = path
cmd = cmd[4..-1].tr('_', '-')
args = args.flatten.compact.map {|s| "'" + s.to_s.gsub("'", "'\\\\''") + "'" }.join(' ')
cmdline = "git #{cmd} #{args} 2>&1"

@logger.debug "gitrb: #{cmdline}"

# Read in binary mode (ascii-8bit) and convert afterwards
out = if block_given?
IO.popen(cmdline, 'rb', &block)
else
set_encoding IO.popen(cmdline, 'rb') {|io| io.read }
end
with_git_dir do
# Read in binary mode (ascii-8bit) and convert afterwards
out = if block_given?
IO.popen(cmdline, 'rb', &block)
else
set_encoding IO.popen(cmdline, 'rb') {|io| io.read }
end

if $?.exitstatus > 0
return '' if $?.exitstatus == 1 && out == ''
raise CommandError.new("git #{cmd}", args, out)
end

if $?.exitstatus > 0
return '' if $?.exitstatus == 1 && out == ''
raise CommandError.new("git #{cmd}", args, out)
out
end

out
else
super
end
Expand All @@ -316,6 +317,14 @@ def default_user
User.new(name, email)
end

def with_git_dir
old_path = ENV['GIT_DIR']
ENV['GIT_DIR'] = path
yield
ensure
ENV['GIT_DIR'] = old_path
end

private

def check_git_version
Expand Down
14 changes: 8 additions & 6 deletions test/commit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
repo.root['a'] = Gitrb::Blob.new(:data => "Yay")
commit = repo.commit("Commit Message", author, author)

IO.popen("git log") do |io|
io.gets.should.equal "commit #{commit.id}\n"
io.gets.should.equal "Author: hans <hans@email.de>\n"
io.gets.should.equal "Date: Mon Apr 20 00:00:00 2009 #{Time.now.strftime('%z')}\n"
io.gets.should.equal "\n"
io.gets.should.equal " Commit Message\n"
repo.with_git_dir do
IO.popen("git log") do |io|
io.gets.should.equal "commit #{commit.id}\n"
io.gets.should.equal "Author: hans <hans@email.de>\n"
io.gets.should.equal "Date: Mon Apr 20 00:00:00 2009 #{Time.now.strftime('%z')}\n"
io.gets.should.equal "\n"
io.gets.should.equal " Commit Message\n"
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions test/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
@repo = Gitrb::Repository.new(:path => REPO, :create => true)
end

it 'should set the GIT_DIR environment variable' do
ENV['GIT_DIR'].should.equal nil
repo.with_git_dir do
ENV['GIT_DIR'].should.equal REPO + "/.git"
end
ENV['GIT_DIR'].should.equal nil
end

it 'should put and get objects by sha' do
blob1 = repo.put(Gitrb::Blob.new(:data => 'Hello'))
blob2 = repo.put(Gitrb::Blob.new(:data => 'World'))
Expand Down

0 comments on commit 2e882f4

Please sign in to comment.