diff --git a/lib/git-smart/git_repo.rb b/lib/git-smart/git_repo.rb index 7df60c8..ec2f2db 100644 --- a/lib/git-smart/git_repo.rb +++ b/lib/git-smart/git_repo.rb @@ -68,8 +68,8 @@ def merge_base(ref_a, ref_b) end def exists?(ref) - git('rev-parse', ref) - $?.success? + output, successful = exec_git('rev-parse', ref) + successful end def rev_list(ref_a, ref_b) @@ -137,15 +137,15 @@ def merge_no_ff!(target) # helper methods, left public in case other commands want to use them directly def git(*args) - output = exec_git(*args) - $?.success? ? output : '' + output, successful = exec_git(*args) + successful ? output : '' end def git!(*args) puts "Executing: #{['git', *args].join(" ")}" - output = exec_git(*args) + output, successful = exec_git(*args) to_display = output.split("\n").map { |l| " #{l}" }.join("\n") - $?.success? ? puts(to_display) : raise(GitSmart::UnexpectedOutput.new(to_display)) + successful ? puts(to_display) : raise(GitSmart::UnexpectedOutput.new(to_display)) output end @@ -164,9 +164,10 @@ def config(name) private def exec_git(*args) - return if @dir.empty? + return '', false if @dir.empty? Dir.chdir(@dir) { - SafeShell.execute('git', *args) + output, exit_status = SafeShell.execute('git', *args) + return output, exit_status.success? } end end diff --git a/lib/git-smart/safe_shell.rb b/lib/git-smart/safe_shell.rb index cd6d314..df168a1 100644 --- a/lib/git-smart/safe_shell.rb +++ b/lib/git-smart/safe_shell.rb @@ -1,21 +1,21 @@ +require 'open3' + module SafeShell def self.execute(command, *args) - read_end, write_end = IO.pipe - pid = fork do - read_end.close - STDOUT.reopen(write_end) - STDERR.reopen(write_end) - exec(command, *args) + cmd = [].push(command).push(args).join(' ') + result = "" + exit_status = nil + + Open3.popen2e (cmd) do |stdin, out, wait_thr| + exit_status = wait_thr.value + result = out.read end - write_end.close - output = read_end.read - Process.waitpid(pid) - read_end.close - output + + return result, exit_status end def self.execute?(*args) - execute(*args) - $?.success? + output, exit_status = execute(*args) + exit_status.success? end end