diff --git a/README.md b/README.md index 661fad7a..188ab73e 100644 --- a/README.md +++ b/README.md @@ -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() diff --git a/lib/git.rb b/lib/git.rb index 10f25d60..1f81bbca 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -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. # @@ -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. # diff --git a/lib/git/lib.rb b/lib/git/lib.rb index d848348a..8efd05a4 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -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] diff --git a/tests/units/test_git_clone.rb b/tests/units/test_git_clone.rb index 61ed4cf8..0ef25bf9 100644 --- a/tests/units/test_git_clone.rb +++ b/tests/units/test_git_clone.rb @@ -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