Skip to content

Commit abfcf94

Browse files
committed
fix: fix Rubocop Metrics/CyclomaticComplexity offense
1 parent 5dd5e0c commit abfcf94

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

.rubocop.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ inherit_from: .rubocop_todo.yml
33
inherit_gem:
44
main_branch_shared_rubocop_config: config/rubocop.yml
55

6+
# Don't care about complexity in TestUnit tests
7+
# This should go away when we switch to RSpec
8+
Metrics/CyclomaticComplexity:
9+
Exclude:
10+
- "tests/test_helper.rb"
11+
- "tests/units/**/*"
12+
613
# Don't care so much about length of methods in tests
714
Metrics/MethodLength:
815
Exclude:

.rubocop_todo.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,3 @@ Metrics/AbcSize:
1515
# Configuration parameters: CountComments, CountAsOne.
1616
Metrics/ClassLength:
1717
Max: 1032
18-
19-
# Offense count: 2
20-
# Configuration parameters: AllowedMethods, AllowedPatterns.
21-
Metrics/CyclomaticComplexity:
22-
Max: 8

lib/git/base.rb

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,9 @@ def self.open(working_dir, options = {})
152152
# of the opened working copy or bare repository
153153
#
154154
def initialize(options = {})
155-
if (working_dir = options[:working_directory])
156-
options[:repository] ||= File.join(working_dir, '.git')
157-
options[:index] ||= File.join(options[:repository], 'index')
158-
end
159-
@logger = options[:log] || Logger.new(nil)
160-
@logger.info('Starting Git')
161-
162-
@working_directory = options[:working_directory] ? Git::WorkingDirectory.new(options[:working_directory]) : nil
163-
@repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil
164-
@index = options[:index] ? Git::Index.new(options[:index], false) : nil
155+
options = default_paths(options)
156+
setup_logger(options[:log])
157+
initialize_components(options)
165158
end
166159

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

830823
private
831824

825+
# Sets default paths in the options hash for direct `Git::Base.new` calls
826+
#
827+
# Factory methods like `Git.open` pre-populate these options by calling
828+
# `normalize_paths`, making this a fallback. It avoids mutating the
829+
# original options hash by returning a new one.
830+
#
831+
# @param options [Hash] the original options hash
832+
# @return [Hash] a new options hash with defaults applied
833+
def default_paths(options)
834+
return options unless (working_dir = options[:working_directory])
835+
836+
options.dup.tap do |opts|
837+
opts[:repository] ||= File.join(working_dir, '.git')
838+
opts[:index] ||= File.join(opts[:repository], 'index')
839+
end
840+
end
841+
842+
# Initializes the logger from the provided options
843+
# @param log_option [Logger, nil] The logger instance from options.
844+
def setup_logger(log_option)
845+
@logger = log_option || Logger.new(nil)
846+
@logger.info('Starting Git')
847+
end
848+
849+
# Initializes the core git objects based on the provided options
850+
# @param options [Hash] The processed options hash.
851+
def initialize_components(options)
852+
@working_directory = Git::WorkingDirectory.new(options[:working_directory]) if options[:working_directory]
853+
@repository = Git::Repository.new(options[:repository]) if options[:repository]
854+
@index = Git::Index.new(options[:index], false) if options[:index]
855+
end
856+
832857
# Normalize options before they are sent to Git::Base.new
833858
#
834859
# Updates the options parameter by setting appropriate values for the following keys:

0 commit comments

Comments
 (0)