Skip to content

Commit 5b0e1c8

Browse files
authored
Update Git.clone to set multiple config variables (#653)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 2500bcf commit 5b0e1c8

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ end
188188
g.config('user.name') # returns 'Scott Chacon'
189189
g.config # returns whole config hash
190190

191+
# Configuration can be set when cloning using the :config option.
192+
# This option can be an single configuration String or an Array
193+
# if multiple config items need to be set.
194+
#
195+
g = Git.clone(
196+
git_uri, destination_path,
197+
:config => [
198+
'core.sshCommand=ssh -i /home/user/.ssh/id_rsa',
199+
'submodule.recurse=true'
200+
]
201+
)
202+
191203
g.tags # returns array of Git::Tag objects
192204

193205
g.show()

lib/git.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ def self.bare(git_dir, options = {})
133133
# @option options [String] :branch The name of a branch or tag to checkout
134134
# instead of the default branch.
135135
#
136+
# @option options [Array, String] :config A list of configuration options to
137+
# set on the newly created repository.
138+
#
136139
# @option options [Integer] :depth Create a shallow clone with a history
137140
# truncated to the specified number of commits.
138141
#
@@ -166,6 +169,18 @@ def self.bare(git_dir, options = {})
166169
# @example Create a bare repository in the directory `ruby-git.git`
167170
# git = Git.clone('https://github.com/ruby-git/ruby-git.git', bare: true)
168171
#
172+
# @example Clone a repository and set a single config option
173+
# git = Git.clone(
174+
# 'https://github.com/ruby-git/ruby-git.git',
175+
# config: 'submodule.recurse=true'
176+
# )
177+
#
178+
# @example Clone a repository and set multiple config options
179+
# git = Git.clone(
180+
# 'https://github.com/ruby-git/ruby-git.git',
181+
# config: ['user.name=John Doe', 'user.email=john@doe.com']
182+
# )
183+
#
169184
# @return [Git::Base] an object that can execute git commands in the context
170185
# of the cloned local working copy or cloned repository.
171186
#

lib/git/lib.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def clone(repository_url, directory, opts = {})
101101
arr_opts << '--bare' if opts[:bare]
102102
arr_opts << '--branch' << opts[:branch] if opts[:branch]
103103
arr_opts << '--depth' << opts[:depth].to_i if opts[:depth] && opts[:depth].to_i > 0
104-
arr_opts << '--config' << opts[:config] if opts[:config]
104+
Array(opts[:config]).each { |c| arr_opts << '--config' << c }
105105
arr_opts << '--origin' << opts[:remote] || opts[:origin] if opts[:remote] || opts[:origin]
106106
arr_opts << '--recursive' if opts[:recursive]
107107
arr_opts << '--mirror' if opts[:mirror]

tests/units/test_git_clone.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,54 @@ def test_git_clone_with_no_name
3333
assert_equal(expected_dir, git.dir.to_s)
3434
end
3535
end
36+
37+
test 'clone with single config option' do
38+
repository_url = 'https://github.com/ruby-git/ruby-git.git'
39+
destination = 'ruby-git'
40+
41+
actual_command_line = nil
42+
43+
in_temp_dir do |path|
44+
git = Git.init('.')
45+
46+
# Mock the Git::Lib#command method to capture the actual command line args
47+
git.lib.define_singleton_method(:command) do |cmd, *opts, &block|
48+
actual_command_line = [cmd, *opts.flatten]
49+
end
50+
51+
git.lib.clone(repository_url, destination, { config: 'user.name=John Doe' })
52+
end
53+
54+
expected_command_line = ['clone', '--config', 'user.name=John Doe', '--', repository_url, destination]
55+
56+
assert_equal(expected_command_line, actual_command_line)
57+
end
58+
59+
test 'clone with multiple config options' do
60+
repository_url = 'https://github.com/ruby-git/ruby-git.git'
61+
destination = 'ruby-git'
62+
63+
actual_command_line = nil
64+
65+
in_temp_dir do |path|
66+
git = Git.init('.')
67+
68+
# Mock the Git::Lib#command method to capture the actual command line args
69+
git.lib.define_singleton_method(:command) do |cmd, *opts, &block|
70+
actual_command_line = [cmd, *opts.flatten]
71+
end
72+
73+
git.lib.clone(repository_url, destination, { config: ['user.name=John Doe', 'user.email=john@doe.com'] })
74+
end
75+
76+
expected_command_line = [
77+
'clone',
78+
'--config', 'user.name=John Doe',
79+
'--config', 'user.email=john@doe.com',
80+
'--', repository_url, destination
81+
]
82+
83+
assert_equal(expected_command_line, actual_command_line)
84+
end
85+
3686
end

0 commit comments

Comments
 (0)