Skip to content

Removed all Dir.chdir calls from private use #195

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

Closed
wants to merge 11 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
54 changes: 27 additions & 27 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'tempfile'
require 'open3' unless (RUBY_VERSION.to_f < 1.9)

module Git

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -334,31 +333,15 @@ 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)
command('config', ['--global', '--get', name], false)
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
Expand Down Expand Up @@ -753,17 +736,17 @@ 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
@logger.info(git_cmd)
@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

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions lib/git/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down