Skip to content

Commit

Permalink
capture stderr and log it if debug is true when running commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rsanheim committed Jul 7, 2008
1 parent 0b5bedf commit 06bae5a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
24 changes: 14 additions & 10 deletions lib/grit/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ def run(prefix, cmd, postfix, options, args)

call = "#{prefix}#{Git.git_binary} --git-dir='#{self.git_dir}' #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{postfix}"
Grit.log(call) if Grit.debug
response = timeout ? sh(call) : wild_sh(call)
response, err = timeout ? sh(call) : wild_sh(call)
Grit.log(response) if Grit.debug
Grit.log(err) if Grit.debug
response
end

def sh(command)
ret, pid = nil, nil
Open4.popen4(command) do |id, _, io, _|
ret, pid, err = nil, nil, nil
Open4.popen4(command) do |id, _, stdout, stderr|
pid = id
ret = Timeout.timeout(self.class.git_timeout) { io.read }
ret = Timeout.timeout(self.class.git_timeout) { stdout.read }
err = stderr.read
@bytes_read += ret.size

if @bytes_read > 5242880 # 5.megabytes
Expand All @@ -74,9 +76,9 @@ def sh(command)
raise GitTimeout.new(command, bytes)
end
end
ret
[ret, err]
rescue Errno::ECHILD
ret
[ret, err]
rescue Object => e
Process.kill('KILL', pid) rescue nil
bytes = @bytes_read
Expand All @@ -85,12 +87,14 @@ def sh(command)
end

def wild_sh(command)
ret = nil
Open4.popen4(command) {|pid, _, io, _|
ret = io.read
ret, err = nil, nil
Open4.popen4(command) {|pid, _, stdout, stderr|
ret = stdout.read
err = stderr.read
}
[ret, err]
rescue Errno::ECHILD
ret
[ret, err]
end

# Transform Ruby style options into git command line options
Expand Down
20 changes: 19 additions & 1 deletion test/test_git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@ def setup
@git = Git.new(File.join(File.dirname(__FILE__), *%w[..]))
end

def teardown
Grit.debug = false
end

def test_method_missing
assert_match(/^git version [\w\.]*$/, @git.version)
end

def test_logs_stderr
Grit.debug = true
Grit.stubs(:log)
Grit.expects(:log).with(includes("git: 'bad' is not a git-command"))
@git.bad
end

def testl_logs_stderr_when_skipping_timeout
Grit.debug = true
Grit.stubs(:log)
Grit.expects(:log).with(includes("git: 'bad' is not a git-command"))
@git.bad :timeout => false
end

def test_transform_options
assert_equal ["-s"], @git.transform_options({:s => true})
assert_equal ["-s '5'"], @git.transform_options({:s => 5})
Expand Down Expand Up @@ -46,7 +64,7 @@ def test_raises_on_slow_shell

def test_works_fine_if_quick
output = 'output'
Open4.expects(:popen4).yields( nil, nil, mock(:read => output), nil )
Open4.expects(:popen4).yields( nil, nil, mock(:read => output), stub(:read => nil) )
assert_equal output, @git.something
end
end

0 comments on commit 06bae5a

Please sign in to comment.