Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Grit::Git#native can return [0, out, err] tuple instead of just stdout

  • Loading branch information...
commit 019bae7c61b0236c093e1bb9eef3b134567b9aa7 1 parent 92f1973
@rtomayko rtomayko authored
Showing with 27 additions and 2 deletions.
  1. +14 −2 lib/grit/git.rb
  2. +13 −0 test/test_git.rb
View
16 lib/grit/git.rb
@@ -256,6 +256,9 @@ def apply_patch(head_sha, patch)
# :raise - When set true, commands that exit with a non-zero status
# raise a CommandFailed exception. This option is available only on
# platforms that support fork(2).
+ # :process_info - By default, a single string with output written to
+ # the process's stdout is returned. Setting this option to true
+ # results in a [exitstatus, out, err] tuple being returned instead.
# args - Non-option arguments passed on the command line.
#
# Optionally yields to the block an IO object attached to the child
@@ -264,7 +267,12 @@ def apply_patch(head_sha, patch)
# Examples
# git.native(:rev_list, {:max_count => 10, :header => true}, "master")
#
- # Returns a String with all output written to the child process's stdout.
+ # Returns a String with all output written to the child process's stdout
+ # when the :process_info option is not set.
+ # Returns a [exitstatus, out, err] tuple when the :process_info option is
+ # set. The exitstatus is an small integer that was the process's exit
+ # status. The out and err elements are the data written to stdout and
+ # stderr as Strings.
# Raises Grit::Git::GitTimeout when the timeout is exceeded or when more
# than Grit::Git.git_max_size bytes are output.
# Raises Grit::Git::CommandFailed when the :raise option is set true and the
@@ -279,6 +287,7 @@ def native(cmd, options = {}, *args, &block)
# special option arguments
env = options.delete(:env) || {}
raise_errors = options.delete(:raise)
+ process_info = options.delete(:process_info)
# fall back to using a shell when the last argument looks like it wants to
# start a pipeline for compatibility with previous versions of grit.
@@ -308,11 +317,14 @@ def native(cmd, options = {}, *args, &block)
:timeout => (Grit::Git.git_timeout if timeout == true),
:max => (Grit::Git.git_max_size if timeout == true)
)
- status = process.status
Grit.log(process.out) if Grit.debug
Grit.log(process.err) if Grit.debug
+
+ status = process.status
if raise_errors && !status.success?
raise CommandFailed.new(argv.join(' '), status.exitstatus, process.err)
+ elsif process_info
+ [status.exitstatus, process.out, process.err]
else
process.out
end
View
13 test/test_git.rb
@@ -114,6 +114,19 @@ def test_passing_env_to_native
@git.native(:help, {:a => true, :env => { 'A' => 'B' }})
end
+ def test_native_process_info_option_on_failure
+ exitstatus, out, err = @git.no_such_command({:process_info => true})
+ assert_equal 1, exitstatus
+ assert !err.empty?
+ end
+
+ def test_native_process_info_option_on_success
+ exitstatus, out, err = @git.help({:process_info => true})
+ assert_equal 0, exitstatus
+ assert !out.empty?
+ assert err.empty?
+ end
+
def test_raising_exceptions_when_native_git_commands_fail
assert_raise Grit::Git::CommandFailed do
@git.native(:bad, {:raise => true})
Please sign in to comment.
Something went wrong with that request. Please try again.