From aa9cf01ba756e9ae41965eea0b666fa9dc26a837 Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Tue, 13 Jun 2017 15:18:52 +0800 Subject: [PATCH 1/5] moves shellout method to Utils::Shell --- lib/redmine/scm/adapters/abstract_adapter.rb | 7 +------ lib/redmine/utils.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/redmine/scm/adapters/abstract_adapter.rb b/lib/redmine/scm/adapters/abstract_adapter.rb index ecebba5..0e9d4c1 100644 --- a/lib/redmine/scm/adapters/abstract_adapter.rb +++ b/lib/redmine/scm/adapters/abstract_adapter.rb @@ -238,12 +238,7 @@ module Redmine end end begin - mode = "r+" - IO.popen(cmd, mode) do |io| - io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding) - io.close_write unless options[:write_stdin] - block.call(io) if block_given? - end + Redmine::Utils::Shell.shellout(cmd, options, &block) ## If scm command does not exist, ## Linux JRuby 1.6.2 (ruby-1.8.7-p330) raises java.io.IOException ## in production environment. diff --git a/lib/redmine/utils.rb b/lib/redmine/utils.rb index b998499..6c1e996 100644 --- a/lib/redmine/utils.rb +++ b/lib/redmine/utils.rb @@ -82,6 +82,20 @@ module Redmine shell_quote(command) end end + + # Executes the given command through IO.popen and yields an IO object + # representing STDIN / STDOUT + # + # Due to how popen works the command will be executed directly without + # involving the shell if cmd is an array. + def shellout(cmd, options = {}, &block) + mode = "r+" + IO.popen(cmd, mode) do |io| + io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding) + io.close_write unless options[:write_stdin] + block.call(io) if block_given? + end + end end module DateCalculation -- 2.1.4