Skip to content

Fix Rubocop Metrics/AbcSize offense #824

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 1 commit into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ inherit_from: .rubocop_todo.yml
inherit_gem:
main_branch_shared_rubocop_config: config/rubocop.yml

# Don't care about complexity in TestUnit tests
# This should go away when we switch to RSpec
# Don't care about CyclomaticComplexity or AbcSize in TestUnit tests This should go
# away when we switch to RSpec.
Metrics/CyclomaticComplexity:
Exclude:
- "tests/test_helper.rb"
- "tests/units/**/*"

Metrics/AbcSize:
Exclude:
- "tests/test_helper.rb"
- "tests/units/**/*"

# Don't care so much about length of methods in tests
Metrics/MethodLength:
Exclude:
Expand Down
5 changes: 0 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 50
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 109

# Offense count: 21
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Expand Down
55 changes: 29 additions & 26 deletions lib/git/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,50 +278,53 @@ def build_git_cmd(args)
#
def process_result(result, normalize, chomp, timeout)
command = result.command
processed_out, processed_err = post_process_all([result.stdout, result.stderr], normalize, chomp)
processed_out, processed_err = post_process_output(result, normalize, chomp)
log_result(result, command, processed_out, processed_err)
command_line_result(command, result, processed_out, processed_err, timeout)
end

def log_result(result, command, processed_out, processed_err)
logger.info { "#{command} exited with status #{result}" }
logger.debug { "stdout:\n#{processed_out.inspect}\nstderr:\n#{processed_err.inspect}" }
end

def command_line_result(command, result, processed_out, processed_err, timeout)
Git::CommandLineResult.new(command, result, processed_out, processed_err).tap do |processed_result|
raise Git::TimeoutError.new(processed_result, timeout) if result.timeout?

raise Git::SignaledError, processed_result if result.signaled?

raise Git::FailedError, processed_result unless result.success?
end
end

# Post-process command output and return an array of the results
#
# @param raw_outputs [Array] the output to post-process
# @param normalize [Boolean] whether to normalize the output of each writer
# @param chomp [Boolean] whether to chomp the output of each writer
# Post-process and return an array of raw output strings
#
# @return [Array<String, nil>] the processed output of each command output object that supports `#string`
# For each raw output string:
#
# @api private
# * If normalize: is true, normalize the encoding by transcoding each line from
# the detected encoding to UTF-8.
# * If chomp: is true chomp the output after normalization.
#
def post_process_all(raw_outputs, normalize, chomp)
raw_outputs.map { |raw_output| post_process(raw_output, normalize, chomp) }
end

# Determine the output to return in the `CommandLineResult`
# Even if no post-processing is done based on the options, the strings returned
# are a copy of the raw output strings. The raw output strings are not modified.
#
# If the writer can return the output by calling `#string` (such as a StringIO),
# then return the result of normalizing the encoding and chomping the output
# as requested.
# @param result [ProcessExecuter::ResultWithCapture] the command's output to post-process
#
# If the writer does not support `#string`, then return nil. The output is
# assumed to be collected by the writer itself such as when the writer
# is a file instead of a StringIO.
# @param normalize [Boolean] whether to normalize the output of each writer
# @param chomp [Boolean] whether to chomp the output of each writer
#
# @param raw_output [#string] the output to post-process
# @return [String, nil]
# @return [Array<String>]
#
# @api private
#
def post_process(raw_output, normalize, chomp)
output = raw_output.dup
output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize
output.chomp! if chomp
output
def post_process_output(result, normalize, chomp)
[result.stdout, result.stderr].map do |raw_output|
output = raw_output.dup
output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize
output.chomp! if chomp
output
end
end
end
end