From f70e961869847fd3c6f6deb4181c6f319fb32f64 Mon Sep 17 00:00:00 2001 From: erxiangbo Date: Tue, 24 Jan 2017 10:42:49 +0800 Subject: [PATCH] make lib.command public --- lib/git/lib.rb | 73 +++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 777e42ea..31a4a911 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -849,6 +849,42 @@ def meets_required_version? (self.current_command_version <=> self.required_command_version) >= 0 end + def command(cmd, opts = [], chdir = true, redirect = '', &block) + global_opts = [] + global_opts << "--git-dir=#{@git_dir}" if !@git_dir.nil? + global_opts << "--work-tree=#{@git_work_dir}" if !@git_work_dir.nil? + + opts = [opts].flatten.map {|s| escape(s) }.join(' ') + + global_opts = global_opts.flatten.map {|s| escape(s) }.join(' ') + + git_cmd = "#{Git::Base.config.binary_path} #{global_opts} #{cmd} #{opts} #{redirect} 2>&1" + + output = nil + + command_thread = nil; + + exitstatus = nil + + with_custom_env_variables do + command_thread = Thread.new do + output = run_command(git_cmd, &block) + exitstatus = $?.exitstatus + end + command_thread.join + end + + if @logger + @logger.info(git_cmd) + @logger.debug(output) + end + + if exitstatus > 1 || (exitstatus == 1 && output != '') + raise Git::GitExecuteError.new(git_cmd + ':' + output.to_s) + end + + return output + end private @@ -903,43 +939,6 @@ def with_custom_env_variables(&block) restore_git_system_env_variables() end - def command(cmd, opts = [], chdir = true, redirect = '', &block) - global_opts = [] - global_opts << "--git-dir=#{@git_dir}" if !@git_dir.nil? - global_opts << "--work-tree=#{@git_work_dir}" if !@git_work_dir.nil? - - opts = [opts].flatten.map {|s| escape(s) }.join(' ') - - global_opts = global_opts.flatten.map {|s| escape(s) }.join(' ') - - git_cmd = "#{Git::Base.config.binary_path} #{global_opts} #{cmd} #{opts} #{redirect} 2>&1" - - output = nil - - command_thread = nil; - - exitstatus = nil - - with_custom_env_variables do - command_thread = Thread.new do - output = run_command(git_cmd, &block) - exitstatus = $?.exitstatus - end - command_thread.join - end - - if @logger - @logger.info(git_cmd) - @logger.debug(output) - end - - if exitstatus > 1 || (exitstatus == 1 && output != '') - raise Git::GitExecuteError.new(git_cmd + ':' + output.to_s) - end - - return output - end - # Takes the diff command line output (as Array) and parse it into a Hash # # @param [String] diff_command the diff commadn to be used