Skip to content

Create a null logger if a logger is not provided #619

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
Feb 18, 2023
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: 3 additions & 6 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'git/base/factory'
require 'logger'

module Git
# Git::Base is the main public interface for interacting with Git commands.
Expand Down Expand Up @@ -90,12 +91,8 @@ def initialize(options = {})
options[:repository] ||= File.join(working_dir, '.git')
options[:index] ||= File.join(options[:repository], 'index')
end
if options[:log]
@logger = options[:log]
@logger.info("Starting Git")
else
@logger = nil
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
Expand Down
9 changes: 4 additions & 5 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'logger'
require 'tempfile'
require 'zlib'

Expand Down Expand Up @@ -52,6 +53,7 @@ def initialize(base = nil, logger = nil)
@git_index_file = nil
@git_work_dir = nil
@path = nil
@logger = logger || Logger.new(nil)

if base.is_a?(Git::Base)
@git_dir = base.repo.path
Expand All @@ -62,7 +64,6 @@ def initialize(base = nil, logger = nil)
@git_index_file = base[:index]
@git_work_dir = base[:working_directory]
end
@logger = logger
end

# creates or reinitializes the repository
Expand Down Expand Up @@ -1137,10 +1138,8 @@ def command(*cmd, redirect: '', chomp: true, &block)
command_thread.join
end

if @logger
@logger.info(git_cmd)
@logger.debug(output)
end
@logger.info(git_cmd)
@logger.debug(output)

raise Git::GitExecuteError, "#{git_cmd}:#{output}" if
exitstatus > 1 || (exitstatus == 1 && output != '')
Expand Down
1 change: 0 additions & 1 deletion tests/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'date'
require 'fileutils'
require 'logger'
require 'minitar'
require 'test/unit'

Expand Down
5 changes: 2 additions & 3 deletions tests/units/test_init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,13 @@ def test_git_clone_config
end
end

# If the :log option is not passed to Git.clone, the result should not
# have a logger
# If the :log option is not passed to Git.clone, a Logger will be created
#
def test_git_clone_without_log
in_temp_dir do |path|
g = Git.clone(BARE_REPO_PATH, 'bare-co')
actual_logger = g.instance_variable_get(:@logger)
assert_equal(nil, actual_logger)
assert_equal(Logger, actual_logger.class)
end
end

Expand Down