Skip to content

Commit a1202eb

Browse files
authored
Allow a logger to be passed to Git.clone (ruby-git#501)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 246af64 commit a1202eb

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ g = Git.clone(URI, NAME, :path => '/tmp/checkout')
184184
g.config('user.name', 'Scott Chacon')
185185
g.config('user.email', 'email@email.com')
186186

187+
# Clone can take an optional logger
188+
logger = Logger.new
189+
g = Git.clone(URI, NAME, :log => logger)
190+
187191
g.add # git add -- "."
188192
g.add(:all=>true) # git add --all -- "."
189193
g.add('file_path') # git add -- "file_path"

lib/git/base.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def self.bare(git_dir, opts = {})
2525
# :index_file
2626
#
2727
def self.clone(repository, name, opts = {})
28-
# run git-clone
29-
self.new(Git::Lib.new.clone(repository, name, opts))
28+
self.new(Git::Lib.new(nil, opts[:log]).clone(repository, name, opts))
3029
end
3130

3231
# Returns (and initialize if needed) a Git::Config instance

lib/git/lib.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,16 @@ def clone(repository, name, opts = {})
7878

7979
command('clone', arr_opts)
8080

81-
(opts[:bare] or opts[:mirror]) ? {:repository => clone_dir} : {:working_directory => clone_dir}
81+
return_base_opts_from_clone(clone_dir, opts)
8282
end
8383

84+
def return_base_opts_from_clone(clone_dir, opts)
85+
base_opts = {}
86+
base_opts[:repository] = clone_dir if (opts[:bare] || opts[:mirror])
87+
base_opts[:working_directory] = clone_dir unless (opts[:bare] || opts[:mirror])
88+
base_opts[:log] = opts[:log] if opts[:log]
89+
base_opts
90+
end
8491

8592
## READ COMMANDS ##
8693

tests/units/test_init.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env ruby
22

33
require File.dirname(__FILE__) + '/../test_helper'
4+
require 'stringio'
5+
require 'logger'
46

57
class TestInit < Test::Unit::TestCase
68
def setup
@@ -99,6 +101,36 @@ def test_git_clone_config
99101
end
100102
end
101103

104+
# If the :log option is not passed to Git.clone, the result should not
105+
# have a logger
106+
#
107+
def test_git_clone_without_log
108+
in_temp_dir do |path|
109+
g = Git.clone(@wbare, 'bare-co')
110+
actual_logger = g.instance_variable_get(:@logger)
111+
assert_equal(nil, actual_logger)
112+
end
113+
end
114+
115+
# If the :log option is passed to Git.clone, the result should have
116+
# a logger set to the value of :log
117+
#
118+
def test_git_clone_log
119+
log_io = StringIO.new
120+
expected_logger = Logger.new(log_io)
121+
122+
in_temp_dir do |path|
123+
g = Git.clone(@wbare, 'bare-co', { log: expected_logger })
124+
actual_logger = g.instance_variable_get(:@logger)
125+
assert_equal(expected_logger.object_id, actual_logger.object_id)
126+
127+
# Ensure that both the clone and Git::Base creation are logged to the logger
128+
#
129+
assert_includes(log_io.string, "Cloning into 'bare-co'...")
130+
assert_includes(log_io.string, 'Starting Git')
131+
end
132+
end
133+
102134
# trying to open a git project using a bare repo - rather than using Git.repo
103135
def test_git_open_error
104136
assert_raise ArgumentError do

0 commit comments

Comments
 (0)