diff --git a/lib/git/base.rb b/lib/git/base.rb index ad3e52ec..1f554e25 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -26,7 +26,7 @@ def self.bare(git_dir, opts = {}) # def self.clone(repository, name, opts = {}) # run git-clone - self.new(Git::Lib.new.clone(repository, name, opts)) + self.new(Git::Lib.new(env: opts.delete(:env)).clone(repository, name, opts)) end # Returns (and initialize if needed) a Git::Config instance @@ -75,6 +75,8 @@ def self.open(working_dir, opts={}) self.new({:working_directory => working_dir}.merge(opts)) end + attr_reader :env + def initialize(options = {}) if working_dir = options[:working_directory] options[:repository] ||= File.join(working_dir, '.git') @@ -90,6 +92,7 @@ def initialize(options = {}) @working_directory = options[:working_directory] ? Git::WorkingDirectory.new(options[:working_directory]) : nil @repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil @index = options[:index] ? Git::Index.new(options[:index], false) : nil + @env = (options[:env] || {}).freeze end # changes current working directory for a block diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 777e42ea..4972be90 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -19,11 +19,14 @@ def initialize(base = nil, logger = nil) @git_dir = base.repo.path @git_index_file = base.index.path if base.index @git_work_dir = base.dir.path if base.dir + @git_extra_env = base.env elsif base.is_a?(Hash) @git_dir = base[:repository] @git_index_file = base[:index] @git_work_dir = base[:working_directory] + @git_extra_env = base[:env].freeze end + @git_extra_env ||= {}.freeze @logger = logger end @@ -64,6 +67,7 @@ def clone(repository, name, 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 << '--no-single-branch' if opts[:no_single_branch] arr_opts << '--config' << opts[:config] if opts[:config] arr_opts << '--origin' << opts[:remote] || opts[:origin] if opts[:remote] || opts[:origin] arr_opts << '--recursive' if opts[:recursive] @@ -75,7 +79,7 @@ def clone(repository, name, opts = {}) command('clone', arr_opts) - opts[:bare] ? {:repository => clone_dir} : {:working_directory => clone_dir} + opts[:bare] ? {:repository => clone_dir} : {:working_directory => clone_dir, :env => @git_extra_env} end @@ -577,6 +581,7 @@ def revert(commitish, opts = {}) arr_opts = [] arr_opts << '--no-edit' if opts[:no_edit] + arr_opts << '--no-commit' if opts[:no_commit] arr_opts << commitish command('revert', arr_opts) @@ -725,8 +730,10 @@ def tag(name, *opts) def fetch(remote, opts) arr_opts = [remote] + arr_opts << opts[:refs] if opts[:refs] arr_opts << '--tags' if opts[:t] || opts[:tags] arr_opts << '--prune' if opts[:p] || opts[:prune] + arr_opts << '--unshallow' if opts[:unshallow] command('fetch', arr_opts) end @@ -850,7 +857,7 @@ def meets_required_version? end - private + protected # Systen ENV variables involved in the git commands. # @@ -869,14 +876,14 @@ def command_lines(cmd, opts = [], chdir = true, redirect = '') # Takes the current git's system ENV variables and store them. def store_git_system_env_variables @git_system_env_variables = {} - ENV_VARIABLE_NAMES.each do |env_variable_name| + (ENV_VARIABLE_NAMES + @git_extra_env.keys).each do |env_variable_name| @git_system_env_variables[env_variable_name] = ENV[env_variable_name] end end # Takes the previously stored git's ENV variables and set them again on ENV. def restore_git_system_env_variables - ENV_VARIABLE_NAMES.each do |env_variable_name| + (ENV_VARIABLE_NAMES + @git_extra_env.keys).each do |env_variable_name| ENV[env_variable_name] = @git_system_env_variables[env_variable_name] end end @@ -887,6 +894,9 @@ def set_custom_git_env_variables ENV['GIT_WORK_TREE'] = @git_work_dir ENV['GIT_INDEX_FILE'] = @git_index_file ENV['GIT_SSH'] = Git::Base.config.git_ssh + @git_extra_env.each do |k, v| + ENV[k] = v + end end # Runs a block inside an environment with customized ENV variables.