Skip to content

Commit

Permalink
Test using Open3.popen2 instead of IO.popen
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouball committed Dec 27, 2023
1 parent 4a969a0 commit 106d8e8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
21 changes: 18 additions & 3 deletions lib/git/base.rb
Expand Up @@ -69,11 +69,26 @@ def self.root_of_worktree(working_dir)
status = nil

git_cmd = "#{Git::Base.config.binary_path} -c core.quotePath=true -c color.ui=false rev-parse --show-toplevel 2>&1"
IO.popen(git_cmd, :chdir => working_dir) do |io|
status = Process.wait2(io.pid).last
result = io.read.chomp

# Option 1 using IO.popen
#
# 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.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

raise ArgumentError, "'#{working_dir}' is not in a git working tree" unless status.success?
result
end
Expand Down
15 changes: 14 additions & 1 deletion lib/git/lib.rb
Expand Up @@ -1255,7 +1255,20 @@ def run_command(git_cmd, chdir=nil, &block)
opts = {}
opts[:chdir] = File.expand_path(chdir) if chdir

[IO.popen(custom_git_env_variables, git_cmd, opts, &block), $?]
# Option 1 using IO.popen
#
# [IO.popen(custom_git_env_variables, git_cmd, opts, &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

# 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]
end

def escape(s)
Expand Down

0 comments on commit 106d8e8

Please sign in to comment.