Skip to content

Update Git.clone to set multiple config variables #653

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
Mar 7, 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ end
g.config('user.name') # returns 'Scott Chacon'
g.config # returns whole config hash

# Configuration can be set when cloning using the :config option.
# This option can be an single configuration String or an Array
# if multiple config items need to be set.
#
g = Git.clone(
git_uri, destination_path,
:config => [
'core.sshCommand=ssh -i /home/user/.ssh/id_rsa',
'submodule.recurse=true'
]
)

g.tags # returns array of Git::Tag objects

g.show()
Expand Down
15 changes: 15 additions & 0 deletions lib/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def self.bare(git_dir, options = {})
# @option options [String] :branch The name of a branch or tag to checkout
# instead of the default branch.
#
# @option options [Array, String] :config A list of configuration options to
# set on the newly created repository.
#
# @option options [Integer] :depth Create a shallow clone with a history
# truncated to the specified number of commits.
#
Expand Down Expand Up @@ -166,6 +169,18 @@ def self.bare(git_dir, options = {})
# @example Create a bare repository in the directory `ruby-git.git`
# git = Git.clone('https://github.com/ruby-git/ruby-git.git', bare: true)
#
# @example Clone a repository and set a single config option
# git = Git.clone(
# 'https://github.com/ruby-git/ruby-git.git',
# config: 'submodule.recurse=true'
# )
#
# @example Clone a repository and set multiple config options
# git = Git.clone(
# 'https://github.com/ruby-git/ruby-git.git',
# config: ['user.name=John Doe', 'user.email=john@doe.com']
# )
#
# @return [Git::Base] an object that can execute git commands in the context
# of the cloned local working copy or cloned repository.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def clone(repository_url, directory, opts = {})
arr_opts << '--bare' if opts[:bare]
arr_opts << '--branch' << opts[:branch] if opts[:branch]
arr_opts << '--depth' << opts[:depth].to_i if opts[:depth] && opts[:depth].to_i > 0
arr_opts << '--config' << opts[:config] if opts[:config]
Array(opts[:config]).each { |c| arr_opts << '--config' << c }
arr_opts << '--origin' << opts[:remote] || opts[:origin] if opts[:remote] || opts[:origin]
arr_opts << '--recursive' if opts[:recursive]
arr_opts << '--mirror' if opts[:mirror]
Expand Down
50 changes: 50 additions & 0 deletions tests/units/test_git_clone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,54 @@ def test_git_clone_with_no_name
assert_equal(expected_dir, git.dir.to_s)
end
end

test 'clone with single config option' do
repository_url = 'https://github.com/ruby-git/ruby-git.git'
destination = 'ruby-git'

actual_command_line = nil

in_temp_dir do |path|
git = Git.init('.')

# Mock the Git::Lib#command method to capture the actual command line args
git.lib.define_singleton_method(:command) do |cmd, *opts, &block|
actual_command_line = [cmd, *opts.flatten]
end

git.lib.clone(repository_url, destination, { config: 'user.name=John Doe' })
end

expected_command_line = ['clone', '--config', 'user.name=John Doe', '--', repository_url, destination]

assert_equal(expected_command_line, actual_command_line)
end

test 'clone with multiple config options' do
repository_url = 'https://github.com/ruby-git/ruby-git.git'
destination = 'ruby-git'

actual_command_line = nil

in_temp_dir do |path|
git = Git.init('.')

# Mock the Git::Lib#command method to capture the actual command line args
git.lib.define_singleton_method(:command) do |cmd, *opts, &block|
actual_command_line = [cmd, *opts.flatten]
end

git.lib.clone(repository_url, destination, { config: ['user.name=John Doe', 'user.email=john@doe.com'] })
end

expected_command_line = [
'clone',
'--config', 'user.name=John Doe',
'--config', 'user.email=john@doe.com',
'--', repository_url, destination
]

assert_equal(expected_command_line, actual_command_line)
end

end