@@ -152,16 +152,9 @@ def self.open(working_dir, options = {})
152
152
# of the opened working copy or bare repository
153
153
#
154
154
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 )
165
158
end
166
159
167
160
# 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)
829
822
830
823
private
831
824
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
+
832
857
# Normalize options before they are sent to Git::Base.new
833
858
#
834
859
# Updates the options parameter by setting appropriate values for the following keys:
0 commit comments