Skip to content

Commit 106d8e8

Browse files
committed
Test using Open3.popen2 instead of IO.popen
1 parent 4a969a0 commit 106d8e8

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/git/base.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,26 @@ def self.root_of_worktree(working_dir)
6969
status = nil
7070

7171
git_cmd = "#{Git::Base.config.binary_path} -c core.quotePath=true -c color.ui=false rev-parse --show-toplevel 2>&1"
72-
IO.popen(git_cmd, :chdir => working_dir) do |io|
73-
status = Process.wait2(io.pid).last
74-
result = io.read.chomp
72+
73+
# Option 1 using IO.popen
74+
#
75+
# IO.popen(git_cmd, :chdir => working_dir) do |io|
76+
# status = Process.wait2(io.pid).last
77+
# result = io.read.chomp
78+
# end
79+
80+
# Option 2 using Open3.popen2
81+
#
82+
Open3.popen2(git_cmd, chdir: working_dir) do |stdin, stdout, wait_thr|
83+
status = wait_thr.value
84+
result = stdout.read.chomp
7585
end
7686

87+
# Option 3 using Open3.capture3
88+
#
89+
# stdout_s, stderr_s, status = Open3.capture3(custom_git_env_variables, git_cmd, opts)
90+
# result = status_s
91+
7792
raise ArgumentError, "'#{working_dir}' is not in a git working tree" unless status.success?
7893
result
7994
end

lib/git/lib.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,20 @@ def run_command(git_cmd, chdir=nil, &block)
12551255
opts = {}
12561256
opts[:chdir] = File.expand_path(chdir) if chdir
12571257

1258-
[IO.popen(custom_git_env_variables, git_cmd, opts, &block), $?]
1258+
# Option 1 using IO.popen
1259+
#
1260+
# [IO.popen(custom_git_env_variables, git_cmd, opts, &block), $?]
1261+
1262+
# Option 2 using Open3.popen2
1263+
#
1264+
Open3.popen2(custom_git_env_variables, git_cmd, opts) do |stdin, stdout, wait_thr|
1265+
[block.call(stdout), wait_thr.value]
1266+
end
1267+
1268+
# Option 3 using Open3.capture3
1269+
#
1270+
# stdout_s, stderr_s, status = Open3.capture3(custom_git_env_variables, git_cmd, opts)
1271+
# [block.call(StringIO.new(stdout_s)), status]
12591272
end
12601273

12611274
def escape(s)

0 commit comments

Comments
 (0)