-
Notifications
You must be signed in to change notification settings - Fork 533
Refactor error thrown when a git command fails #622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
module Git | ||
# The result of running a git command | ||
# | ||
# This object stores the Git command executed and its status, stdout, and stderr. | ||
# | ||
# @api public | ||
# | ||
class CommandLineResult | ||
# Create a CommandLineResult object | ||
# | ||
# @example | ||
# `true` | ||
# git_cmd = %w[git version] | ||
# status = $? | ||
# stdout = "git version 2.39.1\n" | ||
# stderr = "" | ||
# result = Git::CommandLineResult.new(git_cmd, status, stdout, stderr) | ||
# | ||
# @param git_cmd [Array<String>] the git command that was executed | ||
# @param status [Process::Status] the status of the process | ||
# @param stdout [String] the output of the process | ||
# @param stderr [String] the error output of the process | ||
# | ||
def initialize(git_cmd, status, stdout, stderr) | ||
@git_cmd = git_cmd | ||
@status = status | ||
@stdout = stdout | ||
@stderr = stderr | ||
end | ||
|
||
# @attribute [r] git_cmd | ||
# | ||
# The git command that was executed | ||
# | ||
# @example | ||
# git_cmd = %w[git version] | ||
# result = Git::CommandLineResult.new(git_cmd, $?, "", "") | ||
# result.git_cmd #=> ["git", "version"] | ||
# | ||
# @return [Array<String>] | ||
# | ||
attr_reader :git_cmd | ||
|
||
# @attribute [r] status | ||
# | ||
# The status of the process | ||
# | ||
# @example | ||
# `true` | ||
# status = $? | ||
# result = Git::CommandLineResult.new(status, "", "") | ||
# result.status #=> #<Process::Status: pid 87859 exit 0> | ||
# | ||
# @return [Process::Status] | ||
# | ||
attr_reader :status | ||
|
||
# @attribute [r] stdout | ||
# | ||
# The output of the process | ||
# | ||
# @example | ||
# stdout = "git version 2.39.1\n" | ||
# result = Git::CommandLineResult.new($?, stdout, "") | ||
# result.stdout #=> "git version 2.39.1\n" | ||
# | ||
# @return [String] | ||
# | ||
attr_reader :stdout | ||
|
||
# @attribute [r] stderr | ||
# | ||
# The error output of the process | ||
# | ||
# @example | ||
# stderr = "Tag not found\n" | ||
# result = Git::CommandLineResult.new($?, "", stderr) | ||
# result.stderr #=> "Tag not found\n" | ||
# | ||
# @return [String] | ||
# | ||
attr_reader :stderr | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'git/git_execute_error' | ||
|
||
module Git | ||
# This error is raised when a git command fails | ||
# | ||
# The git command executed, status, stdout, and stderr are available from this | ||
# object. The #message includes the git command, the status of the process, and | ||
# the stderr of the process. | ||
# | ||
# @api public | ||
# | ||
class FailedError < Git::GitExecuteError | ||
# Create a FailedError object | ||
# | ||
# @example | ||
# `exit 1` # set $? appropriately for this example | ||
# result = Git::CommandLineResult.new(%w[git status], $?, '', "failed") | ||
# error = Git::FailedError.new(result) | ||
# error.message #=> | ||
# "[\"git\", \"status\"]\nstatus: pid 89784 exit 1\nstderr: \"failed\"" | ||
# | ||
# @param result [Git::CommandLineResult] the result of the git command including | ||
# the git command, status, stdout, and stderr | ||
# | ||
def initialize(result) | ||
super("#{result.git_cmd}\nstatus: #{result.status}\nstderr: #{result.stderr.inspect}") | ||
@result = result | ||
end | ||
|
||
# @attribute [r] result | ||
# | ||
# The result of the git command including the git command and its status and output | ||
# | ||
# @example | ||
# `exit 1` # set $? appropriately for this example | ||
# result = Git::CommandLineResult.new(%w[git status], $?, '', "failed") | ||
# error = Git::FailedError.new(result) | ||
# error.result #=> | ||
# #<Git::CommandLineResult:0x00000001046bd488 | ||
# @git_cmd=["git", "status"], | ||
# @status=#<Process::Status: pid 89784 exit 1>, | ||
# @stderr="failed", | ||
# @stdout=""> | ||
# | ||
# @return [Git::CommandLineResult] | ||
# | ||
attr_reader :result | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Git | ||
# This error is raised when a git command fails | ||
# | ||
class GitExecuteError < StandardError; end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'git/git_execute_error' | ||
|
||
module Git | ||
# This error is raised when a git command exits because of an uncaught signal | ||
# | ||
# The git command executed, status, stdout, and stderr are available from this | ||
# object. The #message includes the git command, the status of the process, and | ||
# the stderr of the process. | ||
# | ||
# @api public | ||
# | ||
class SignaledError < Git::GitExecuteError | ||
# Create a SignaledError object | ||
# | ||
# @example | ||
# `kill -9 $$` # set $? appropriately for this example | ||
# result = Git::CommandLineResult.new(%w[git status], $?, '', "killed") | ||
# error = Git::SignaledError.new(result) | ||
# error.message #=> | ||
# "[\"git\", \"status\"]\nstatus: pid 88811 SIGKILL (signal 9)\nstderr: \"killed\"" | ||
# | ||
# @param result [Git::CommandLineResult] the result of the git command including the git command, status, stdout, and stderr | ||
# | ||
def initialize(result) | ||
super("#{result.git_cmd}\nstatus: #{result.status}\nstderr: #{result.stderr.inspect}") | ||
@result = result | ||
end | ||
|
||
# @attribute [r] result | ||
# | ||
# The result of the git command including the git command, status, and output | ||
# | ||
# @example | ||
# `kill -9 $$` # set $? appropriately for this example | ||
# result = Git::CommandLineResult.new(%w[git status], $?, '', "killed") | ||
# error = Git::SignaledError.new(result) | ||
# error.result #=> | ||
# #<Git::CommandLineResult:0x000000010470f6e8 | ||
# @git_cmd=["git", "status"], | ||
# @status=#<Process::Status: pid 88811 SIGKILL (signal 9)>, | ||
# @stderr="killed", | ||
# @stdout=""> | ||
# | ||
# @return [Git::CommandLineResult] | ||
# | ||
attr_reader :result | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'test_helper' | ||
|
||
class TestCommamndLineResult < Test::Unit::TestCase | ||
def test_initialization | ||
git_cmd = Object.new | ||
status = Object.new | ||
stdout = Object.new | ||
stderr = Object.new | ||
|
||
result = Git::CommandLineResult.new(git_cmd, status, stdout, stderr) | ||
|
||
assert_equal(git_cmd, result.git_cmd) | ||
assert_equal(status, result.status) | ||
assert_equal(stdout, result.stdout) | ||
assert_equal(stderr, result.stderr) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'test_helper' | ||
|
||
class TestFailedError < Test::Unit::TestCase | ||
def test_initializer | ||
status = Struct.new(:to_s).new('pid 89784 exit 1') | ||
result = Git::CommandLineResult.new(%w[git status], status, '', "failed") | ||
|
||
error = Git::FailedError.new(result) | ||
|
||
assert(error.is_a?(Git::GitExecuteError)) | ||
assert_equal(result, error.result) | ||
end | ||
|
||
def test_message | ||
status = Struct.new(:to_s).new('pid 89784 exit 1') | ||
result = Git::CommandLineResult.new(%w[git status], status, '', "failed") | ||
|
||
error = Git::FailedError.new(result) | ||
|
||
expected_message = "[\"git\", \"status\"]\nstatus: pid 89784 exit 1\nstderr: \"failed\"" | ||
assert_equal(expected_message, error.message) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'test_helper' | ||
|
||
class TestGitExecuteError < Test::Unit::TestCase | ||
def test_is_a_standard_error | ||
assert(Git::GitExecuteError < StandardError) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'test_helper' | ||
|
||
class TestSignaledError < Test::Unit::TestCase | ||
def test_initializer | ||
status = Struct.new(:to_s).new('pid 65628 SIGKILL (signal 9)') # `kill -9 $$` | ||
result = Git::CommandLineResult.new(%w[git status], status, '', "uncaught signal") | ||
|
||
error = Git::SignaledError.new(result) | ||
|
||
assert(error.is_a?(Git::GitExecuteError)) | ||
assert_equal(result, error.result) | ||
end | ||
|
||
def test_message | ||
status = Struct.new(:to_s).new('pid 65628 SIGKILL (signal 9)') # `kill -9 $$` | ||
result = Git::CommandLineResult.new(%w[git status], status, '', "uncaught signal") | ||
|
||
error = Git::SignaledError.new(result) | ||
|
||
expected_message = "[\"git\", \"status\"]\nstatus: pid 65628 SIGKILL (signal 9)\nstderr: \"uncaught signal\"" | ||
assert_equal(expected_message, error.message) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcouball I think stdout also has to be printed here since stderr is redirected to stdout??
ruby-git/lib/git/lib.rb
Line 1194 in 5b0e1c8