Skip to content

Commit d19b9c8

Browse files
committed
Switched back from open4 to IO.popen.
open4 was added as a dependency when I was trying to parse STDERR and needed to work with STDIN. As it turns out, $stdin is good enough. Redirecting STDERR to STDOUT seems to be good enough as well. While open4 is probably still a cleaner approach, its reliance on fork() means it doesn't work on non-JRuby in Windows (echnically it doesn't work in JRuby either, but JRuby has an IO.popen4 special method that does virtually the same thing and we were using that instead).
1 parent d3570dc commit d19b9c8

File tree

2 files changed

+11
-35
lines changed

2 files changed

+11
-35
lines changed

Rakefile

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ begin
1212
spec.email = "nirvdrum@gmail.com"
1313
spec.license = 'MIT'
1414
spec.add_development_dependency 'minitest'
15-
spec.add_dependency 'open4'
1615
end
1716
Jeweler::GemcutterTasks.new
1817

lib/svn2git/migration.rb

+11-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'optparse'
22
require 'pp'
3-
require 'open4'
43
require 'timeout'
54

65
module Svn2Git
@@ -374,7 +373,6 @@ def run_command(cmd, exit_on_error=true, printout_output=false)
374373
log "Running command: #{cmd}\n"
375374

376375
ret = ''
377-
@mutex ||= Mutex.new
378376
@stdin_queue ||= Queue.new
379377

380378
# We need to fetch input from the user to pass through to the underlying sub-process. We'll constantly listen
@@ -386,44 +384,28 @@ def run_command(cmd, exit_on_error=true, printout_output=false)
386384

387385
# Open4 forks, which JRuby doesn't support. But JRuby added a popen4-compatible method on the IO class,
388386
# so we can use that instead.
389-
status = (defined?(JRUBY_VERSION) ? IO : Open4).popen4(cmd) do |pid, stdin, stdout, stderr|
387+
IO.popen("2>&1 #{cmd}") do |output|
390388
threads = []
391389

392-
threads << Thread.new(stdout) do |stdout|
393-
stdout.each do |line|
394-
@mutex.synchronize do
395-
ret << line
396-
397-
if printout_output
398-
$stdout.print line
399-
else
400-
log line
401-
end
402-
end
403-
end
404-
end
405-
406-
threads << Thread.new(stderr) do |stderr|
390+
threads << Thread.new(output) do |output|
407391
# git-svn seems to do all of its prompting for user input via STDERR. When it prompts for input, it will
408392
# not terminate the line with a newline character, so we can't split the input up by newline. It will,
409393
# however, use a space to separate the user input from the prompt. So we split on word boundaries here
410394
# while draining STDERR.
411-
stderr.each(' ') do |word|
412-
@mutex.synchronize do
413-
ret << word
414-
415-
if printout_output
416-
$stdout.print word
417-
else
418-
log word
419-
end
395+
output.each(' ') do |word|
396+
ret << word
397+
398+
if printout_output
399+
$stdout.print word
400+
else
401+
log word
420402
end
421403
end
422404
end
423405

424406
# Simple pass-through thread to take anything the user types via STDIN and passes it through to the
425407
# sub-process's stdin pipe.
426-
Thread.new(stdin) do |stdin|
408+
Thread.new do
427409
loop do
428410
user_reply = @stdin_queue.pop
429411

@@ -441,12 +423,7 @@ def run_command(cmd, exit_on_error=true, printout_output=false)
441423
@stdin_queue << nil
442424
end
443425

444-
# JRuby's open4 doesn't return a Process::Status object when invoked with a block, but rather returns the
445-
# block expression's value. The Process::Status is stored as $?, so we need to normalize the status
446-
# object if on JRuby.
447-
status = $? if defined?(JRUBY_VERSION)
448-
449-
if exit_on_error && (status.exitstatus != 0)
426+
if exit_on_error && $?.exitstatus != 0
450427
$stderr.puts "command failed:\n#{cmd}"
451428
exit -1
452429
end

0 commit comments

Comments
 (0)