Skip to content

Commit b8c5b79

Browse files
committed
First pass at passing STDIN through to git-svn.
This is JRuby-only currently. We'll need to rely on the open4 lib to get non-JRuby support.
1 parent bbcc35d commit b8c5b79

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

lib/svn2git/migration.rb

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,23 +340,54 @@ def run_command(cmd, exit_on_error=true, printout_output=false)
340340
log "Running command: #{cmd}"
341341

342342
ret = ''
343+
mutex = Mutex.new
343344

344-
cmd = "2>&1 #{cmd}"
345-
IO.popen(cmd) do |stdout|
346-
if printout_output
347-
stdout.each_char do |character|
348-
$stdout.print character
349-
ret << character
350-
end
351-
else
345+
status = IO.popen4(cmd) do |pid, stdin, stdout, stderr|
346+
threads = []
347+
348+
threads << Thread.new(stdout) do |stdout|
352349
stdout.each do |line|
353-
log line
354-
ret << line
350+
mutex.synchronize do
351+
ret << line
352+
353+
if printout_output
354+
$stdout.print line
355+
else
356+
log line
357+
end
358+
end
359+
end
360+
end
361+
362+
threads << Thread.new(stderr) do |stderr|
363+
stderr.each(' ') do |word|
364+
mutex.synchronize do
365+
ret << word
366+
367+
if printout_output
368+
$stdout.print word
369+
else
370+
log word
371+
end
372+
end
355373
end
356374
end
375+
376+
threads << Thread.new(stdin) do |stdin|
377+
user_reply = $stdin.gets.chomp
378+
stdin.puts user_reply
379+
stdin.close
380+
end
381+
382+
threads.each(&:join)
357383
end
358384

359-
if exit_on_error && ($?.exitstatus != 0)
385+
# JRuby's open4 doesn't return a Process::Status object when invoked with a block, but rather returns the
386+
# block expression's value. The Process::Status is stored as $?, so we need to normalize the status
387+
# object if on JRuby.
388+
status = $? if defined?(JRUBY_VERSION)
389+
390+
if exit_on_error && (status.exitstatus != 0)
360391
$stderr.puts "command failed:\n#{cmd}"
361392
exit -1
362393
end

0 commit comments

Comments
 (0)