Skip to content

Commit

Permalink
Use Open3.capture3 instead of IO.popen
Browse files Browse the repository at this point in the history
Signed-off-by: James Couball <jcouball@yahoo.com>
  • Loading branch information
jcouball committed Dec 27, 2023
1 parent 4231e94 commit d684854
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
15 changes: 8 additions & 7 deletions lib/git/base.rb
Expand Up @@ -72,24 +72,25 @@ def self.root_of_worktree(working_dir)

# Option 1 using IO.popen
#
# IO.popen(git_cmd, :chdir => working_dir) do |io|
# IO.popen(git_cmd, chdir: working_dir) do |io|
# status = Process.wait2(io.pid).last
# result = io.read.chomp
# end

# Option 2 using Open3.popen2
#
Open3.popen2(git_cmd, chdir: working_dir) do |stdin, stdout, wait_thr|
status = wait_thr.value
result = stdout.chomp
end
# Open3.popen2(git_cmd, chdir: working_dir) do |stdin, stdout, wait_thr|
# status = wait_thr.value
# result = stdout.read.chomp
# end

# Option 3 using Open3.capture3
#
# stdout_s, stderr_s, status = Open3.capture3(custom_git_env_variables, git_cmd, opts)
# result = status_s
stdout_s, stderr_s, status = Open3.capture3(git_cmd, chdir: working_dir)
result = stdout_s.chomp

raise ArgumentError, "'#{working_dir}' is not in a git working tree" unless status.success?

result
end

Expand Down
11 changes: 6 additions & 5 deletions lib/git/lib.rb
Expand Up @@ -3,6 +3,7 @@
require 'tempfile'
require 'zlib'
require 'open3'
require 'stringio'

module Git
class Lib
Expand Down Expand Up @@ -1261,14 +1262,14 @@ def run_command(git_cmd, chdir=nil, &block)

# Option 2 using Open3.popen2
#
Open3.popen2(custom_git_env_variables, git_cmd, opts) do |stdin, stdout, wait_thr|
[block.call(stdout), wait_thr.value]
end
# Open3.popen2(custom_git_env_variables, git_cmd, opts) do |stdin, stdout, wait_thr|
# [block.call(stdout), wait_thr.value]
# end

# Option 3 using Open3.capture3
#
# stdout_s, stderr_s, status = Open3.capture3(custom_git_env_variables, git_cmd, opts)
# [block.call(StringIO.new(stdout_s)), status]
stdout_s, stderr_s, status = Open3.capture3(custom_git_env_variables, git_cmd, opts)
[block.call(StringIO.new(stdout_s)), status]
end

def escape(s)
Expand Down

0 comments on commit d684854

Please sign in to comment.