From 76610969a8a5be0aa794c89800c1c04e74b81f33 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Wed, 26 Nov 2014 16:19:54 -0500 Subject: [PATCH 01/11] Removed the Dir.chdir in `list_files` --- lib/git/lib.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 34a8b184..9bb282cc 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -233,9 +233,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 From 6d623a693bf40756006ca64e3f14b9e613cfa0c8 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Wed, 26 Nov 2014 16:52:49 -0500 Subject: [PATCH 02/11] Removed a Dir.chdir from `command` --- lib/git/lib.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 9bb282cc..d5f6102c 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -751,9 +751,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 @@ -761,7 +761,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 @@ -822,10 +822,16 @@ def log_path_options(opts) arr_opts end - def run_command(git_cmd, &block) - return IO.popen(git_cmd, &block) if block_given? + 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 def escape(s) From aad98f15d715210a646d281a104c2b953bc574b9 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Wed, 26 Nov 2014 16:55:36 -0500 Subject: [PATCH 03/11] Removed a Dir.chdir from `self.export` --- lib/git.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 3cdbdb0baf4d9f755a80dbdc0fe3e0965415a673 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Thu, 27 Nov 2014 06:59:39 -0500 Subject: [PATCH 04/11] Removed Dir.chdir from `repo_size` --- lib/git/base.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/git/base.rb b/lib/git/base.rb index 362f6432..89b546cf 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -119,9 +119,7 @@ def chdir # :yields: the Git::Path # returns the repository size in bytes def repo_size - Dir.chdir(repo.path) do - return `du -s`.chomp.split.first.to_i - end + return IO.popen("du -s", {chdir: repo.path}).read.chomp.split.first.to_i end #g.config('user.name', 'Scott Chacon') # sets value From 9683a3281fa3ebac9230f1004a7f21b38b903cc8 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Thu, 27 Nov 2014 07:14:36 -0500 Subject: [PATCH 05/11] Removed Dir.chdir from `construct_status` --- lib/git/status.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/git/status.rb b/lib/git/status.rb index d59bc777..74d3034e 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.byteslice(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 From 7d27728331065d24da15a93306c932ffe273009f Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Thu, 27 Nov 2014 20:03:03 -0500 Subject: [PATCH 06/11] Removed Dir.chdir from `config_get`. We know this is safe because a- there is a bug where `build_list` is being called, but that's obviously a bug copied from `config_list`. Also, if `@git_dir` is non-nil, `#command` will already ensure the command is run within `@git_dir` as the working directory. --- lib/git/lib.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index d5f6102c..ce02bdcb 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -332,15 +332,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) From 6d4d46a7b9ec2e492c0515611e55ccfc5e605079 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Thu, 27 Nov 2014 20:03:36 -0500 Subject: [PATCH 07/11] Removed Dir.chdir from `config_list`. We know this is safe because if `@git_dir` is non-nil, `#command_lines` will already ensure the command is run within `@git_dir` as the working directory. --- lib/git/lib.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index ce02bdcb..b6b05ad7 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -340,15 +340,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 From e22392a8d2470206389162bce5b15400b690eb48 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Fri, 28 Nov 2014 07:07:25 -0500 Subject: [PATCH 08/11] Removed modern hash-syntax which broke compatibility for ruby 1.8 --- lib/git/base.rb | 2 +- lib/git/lib.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/git/base.rb b/lib/git/base.rb index 89b546cf..bc0bb43e 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -119,7 +119,7 @@ def chdir # :yields: the Git::Path # returns the repository size in bytes def repo_size - return IO.popen("du -s", {chdir: repo.path}).read.chomp.split.first.to_i + return IO.popen("du -s", {:chdir => repo.path}).read.chomp.split.first.to_i end #g.config('user.name', 'Scott Chacon') # sets value diff --git a/lib/git/lib.rb b/lib/git/lib.rb index b6b05ad7..1291df16 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -808,7 +808,7 @@ def log_path_options(opts) def run_command(git_cmd, chdir = nil, &block) commands = [git_cmd] - commands << {chdir: chdir} unless chdir.nil? + commands << {:chdir => chdir} unless chdir.nil? if block_given? retval = IO.popen(*commands, &block) return retval, $? From 858ead414f74772a51fd45e3f0c190dc8d08e63d Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Mon, 1 Dec 2014 10:14:17 -0500 Subject: [PATCH 09/11] Removed `String#byteslice` call for 1.9.2 compatibility. --- lib/git/status.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/git/status.rb b/lib/git/status.rb index 74d3034e..7347d27a 100644 --- a/lib/git/status.rb +++ b/lib/git/status.rb @@ -91,7 +91,7 @@ def construct_status # find untracked in working dir prefix = "#{@base.dir.path}/" Dir.glob(prefix+"**/*", File::FNM_DOTMATCH) do |abs_file| - file = abs_file.byteslice(prefix.length..-1) + 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} From 70a8371013290470555572c7c2ca36c2201c37a9 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Mon, 1 Dec 2014 11:30:36 -0500 Subject: [PATCH 10/11] Added better 1.8-compatibility, but only by re-adding some `Dir.chdir` calls. --- lib/git/base.rb | 12 ++++++++++-- lib/git/lib.rb | 27 +++++++++++++++++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/git/base.rb b/lib/git/base.rb index bc0bb43e..2f05f0b1 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -118,8 +118,16 @@ def chdir # :yields: the Git::Path end # returns the repository size in bytes - def repo_size - return IO.popen("du -s", {:chdir => repo.path}).read.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 #g.config('user.name', 'Scott Chacon') # sets value diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 1291df16..31958239 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -806,16 +806,27 @@ def log_path_options(opts) arr_opts end - 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, $? + # 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 - out, process_status = Open3.capture2(*commands) + 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) - return out.chomp, process_status + return out.chomp, process_status + end end def escape(s) From 0f78b671f9feb1433b1c9521ad9d7d6d44f5fc35 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Tue, 2 Dec 2014 11:52:10 -0500 Subject: [PATCH 11/11] Added require 'open3' when needed and available. --- lib/git/lib.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 31958239..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