Skip to content

Fix Rubocop Metrics/CyclomaticComplexity offense #823

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
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ 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
Metrics/CyclomaticComplexity:
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 @@ -15,8 +15,3 @@ Metrics/AbcSize:
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 1032

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 8
45 changes: 35 additions & 10 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,9 @@ def self.open(working_dir, options = {})
# of the opened working copy or bare repository
#
def initialize(options = {})
if (working_dir = options[:working_directory])
options[:repository] ||= File.join(working_dir, '.git')
options[:index] ||= File.join(options[:repository], 'index')
end
@logger = options[:log] || Logger.new(nil)
@logger.info('Starting Git')

@working_directory = options[:working_directory] ? Git::WorkingDirectory.new(options[:working_directory]) : nil
@repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil
@index = options[:index] ? Git::Index.new(options[:index], false) : nil
options = default_paths(options)
setup_logger(options[:log])
initialize_components(options)
end

# Update the index from the current worktree to prepare the for the next commit
Expand Down Expand Up @@ -829,6 +822,38 @@ def diff_path_status(objectish = 'HEAD', obj2 = nil)

private

# Sets default paths in the options hash for direct `Git::Base.new` calls
#
# Factory methods like `Git.open` pre-populate these options by calling
# `normalize_paths`, making this a fallback. It avoids mutating the
# original options hash by returning a new one.
#
# @param options [Hash] the original options hash
# @return [Hash] a new options hash with defaults applied
def default_paths(options)
return options unless (working_dir = options[:working_directory])

options.dup.tap do |opts|
opts[:repository] ||= File.join(working_dir, '.git')
opts[:index] ||= File.join(opts[:repository], 'index')
end
end

# Initializes the logger from the provided options
# @param log_option [Logger, nil] The logger instance from options.
def setup_logger(log_option)
@logger = log_option || Logger.new(nil)
@logger.info('Starting Git')
end

# Initializes the core git objects based on the provided options
# @param options [Hash] The processed options hash.
def initialize_components(options)
@working_directory = Git::WorkingDirectory.new(options[:working_directory]) if options[:working_directory]
@repository = Git::Repository.new(options[:repository]) if options[:repository]
@index = Git::Index.new(options[:index], false) if options[:index]
end

# Normalize options before they are sent to Git::Base.new
#
# Updates the options parameter by setting appropriate values for the following keys:
Expand Down