diff --git a/lib/git.rb b/lib/git.rb index 9ef0fc09..38423725 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -99,7 +99,7 @@ def self.export(repository, name, options = {}) options.delete(:remote) repo = clone(repository, name, {:depth => 1}.merge(options)) repo.checkout("origin/#{options[:branch]}") if options[:branch] - Dir.chdir(repo.dir.to_s) { FileUtils.rm_r '.git' } + FileUtils.rm_r "#{repo.dir.to_s}/.git" end # Same as g.config, but forces it to be at the global level diff --git a/lib/git/base.rb b/lib/git/base.rb index 362f6432..2f05f0b1 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -118,9 +118,15 @@ def chdir # :yields: the Git::Path end # returns the repository size in bytes - def repo_size - Dir.chdir(repo.path) do - return `du -s`.chomp.split.first.to_i + if (RUBY_VERSION.to_f < 1.9) + def repo_size + Dir.chdir(repo.path) do + return IO.popen("du -s").read.chomp.split.first.to_i + end + end + else + def repo_size + return IO.popen("du -s", {:chdir => repo.path}).read.chomp.split.first.to_i end end diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 34a8b184..8acf26d8 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -1,4 +1,5 @@ require 'tempfile' +require 'open3' unless (RUBY_VERSION.to_f < 1.9) module Git @@ -233,9 +234,7 @@ def branches_all def list_files(ref_dir) dir = File.join(@git_dir, 'refs', ref_dir) - files = [] - Dir.chdir(dir) { files = Dir.glob('**/*').select { |f| File.file?(f) } } rescue nil - files + Dir.glob("#{dir}/**/*").select { |f| File.file?(f) } rescue nil end def branch_current @@ -334,15 +333,7 @@ def config_remote(name) end def config_get(name) - do_get = lambda do |path| - command('config', ['--get', name]) - end - - if @git_dir - Dir.chdir(@git_dir, &do_get) - else - build_list.call - end + command('config', ['--get', name]) end def global_config_get(name) @@ -350,15 +341,7 @@ def global_config_get(name) end def config_list - build_list = lambda do |path| - parse_config_list command_lines('config', ['--list']) - end - - if @git_dir - Dir.chdir(@git_dir, &build_list) - else - build_list.call - end + parse_config_list command_lines('config', ['--list']) end def global_config_list @@ -753,9 +736,9 @@ def command(cmd, opts = [], chdir = true, redirect = '', &block) output = nil if chdir && (Dir.getwd != path) - Dir.chdir(path) { output = run_command(git_cmd, &block) } + output, process_status = run_command(git_cmd, path, &block) else - output = run_command(git_cmd, &block) + output, process_status = run_command(git_cmd, &block) end if @logger @@ -763,7 +746,7 @@ def command(cmd, opts = [], chdir = true, redirect = '', &block) @logger.debug(output) end - if $?.exitstatus > 1 || ($?.exitstatus == 1 && output != '') + if process_status.exitstatus > 1 || (process_status.exitstatus == 1 && output != '') raise Git::GitExecuteError.new(git_cmd + ':' + output.to_s) end @@ -824,10 +807,27 @@ def log_path_options(opts) arr_opts end - def run_command(git_cmd, &block) - return IO.popen(git_cmd, &block) if block_given? + # run_command + if (RUBY_VERSION.to_f < 1.9) + # in Ruby 1.8 we just have to run inside Dir.chdir. No getting around it + def run_command(git_cmd, chdir = nil, &block) + Dir.chdir(chdir || Dir.getwd) do + return IO.popen(git_cmd, &block), $? if block_given? + return `#{git_cmd}`.chomp, $? + end + end + else + def run_command(git_cmd, chdir = nil, &block) + commands = [git_cmd] + commands << {:chdir => chdir} unless chdir.nil? + if block_given? + retval = IO.popen(*commands, &block) + return retval, $? + end + out, process_status = Open3.capture2(*commands) - `#{git_cmd}`.chomp + return out.chomp, process_status + end end def escape(s) diff --git a/lib/git/status.rb b/lib/git/status.rb index d59bc777..7347d27a 100644 --- a/lib/git/status.rb +++ b/lib/git/status.rb @@ -89,12 +89,12 @@ def construct_status ignore = @base.lib.ignored_files # find untracked in working dir - Dir.chdir(@base.dir.path) do - Dir.glob('**/*', File::FNM_DOTMATCH) do |file| - next if @files[file] || File.directory?(file) || ignore.include?(file) || file =~ /^.git\/.+/ + prefix = "#{@base.dir.path}/" + Dir.glob(prefix+"**/*", File::FNM_DOTMATCH) do |abs_file| + file = abs_file.slice(prefix.length..-1) + next if @files[file] || File.directory?(file) || ignore.include?(file) || file =~ /^.git\/.+/ - @files[file] = {:path => file, :untracked => true} - end + @files[file] = {:path => file, :untracked => true} end # find modified in tree