Skip to content

Commit 9ee7ca9

Browse files
authored
Create a null logger if a logger is not provided (#619)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 872de4c commit 9ee7ca9

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

lib/git/base.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'git/base/factory'
2+
require 'logger'
23

34
module Git
45
# Git::Base is the main public interface for interacting with Git commands.
@@ -90,12 +91,8 @@ def initialize(options = {})
9091
options[:repository] ||= File.join(working_dir, '.git')
9192
options[:index] ||= File.join(options[:repository], 'index')
9293
end
93-
if options[:log]
94-
@logger = options[:log]
95-
@logger.info("Starting Git")
96-
else
97-
@logger = nil
98-
end
94+
@logger = (options[:log] || Logger.new(nil))
95+
@logger.info("Starting Git")
9996

10097
@working_directory = options[:working_directory] ? Git::WorkingDirectory.new(options[:working_directory]) : nil
10198
@repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil

lib/git/lib.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'logger'
12
require 'tempfile'
23
require 'zlib'
34

@@ -52,6 +53,7 @@ def initialize(base = nil, logger = nil)
5253
@git_index_file = nil
5354
@git_work_dir = nil
5455
@path = nil
56+
@logger = logger || Logger.new(nil)
5557

5658
if base.is_a?(Git::Base)
5759
@git_dir = base.repo.path
@@ -62,7 +64,6 @@ def initialize(base = nil, logger = nil)
6264
@git_index_file = base[:index]
6365
@git_work_dir = base[:working_directory]
6466
end
65-
@logger = logger
6667
end
6768

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

1140-
if @logger
1141-
@logger.info(git_cmd)
1142-
@logger.debug(output)
1143-
end
1141+
@logger.info(git_cmd)
1142+
@logger.debug(output)
11441143

11451144
raise Git::GitExecuteError, "#{git_cmd}:#{output}" if
11461145
exitstatus > 1 || (exitstatus == 1 && output != '')

tests/test_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'date'
22
require 'fileutils'
3-
require 'logger'
43
require 'minitar'
54
require 'test/unit'
65

tests/units/test_init.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,13 @@ def test_git_clone_config
113113
end
114114
end
115115

116-
# If the :log option is not passed to Git.clone, the result should not
117-
# have a logger
116+
# If the :log option is not passed to Git.clone, a Logger will be created
118117
#
119118
def test_git_clone_without_log
120119
in_temp_dir do |path|
121120
g = Git.clone(BARE_REPO_PATH, 'bare-co')
122121
actual_logger = g.instance_variable_get(:@logger)
123-
assert_equal(nil, actual_logger)
122+
assert_equal(Logger, actual_logger.class)
124123
end
125124
end
126125

0 commit comments

Comments
 (0)