Skip to content

Isolate Dir.chdir to a new process, or mutex #372

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 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -108,7 +108,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]
FileUtils.rm_r(File.join(repo.dir.to_s, '.git'))
Dir.chdir(repo.dir.to_s) { FileUtils.rm_r '.git' }
end

# Same as g.config, but forces it to be at the global level
Expand Down
39 changes: 8 additions & 31 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
module Git

class Base
# Adding a mutex to the class because each repo should be sharing the same mutex
# in case we need to Dir.chdir and we don't have fork() support to isolate that
class << self
attr_accessor :chdir_semaphore
end
Git::Base.chdir_semaphore = Mutex.new

include Git::Base::Factory

Expand Down Expand Up @@ -98,37 +92,18 @@ def initialize(options = {})
@index = options[:index] ? Git::Index.new(options[:index], false) : nil
end

# changes current working directory for a block to the git working directory.
#
# Note: If we can fork() or spawn(), Dir.chdir will happen in a new process
# otherwise, we will use a mutex to prevent threading errors
# See https://github.com/ruby-git/ruby-git/issues/355 for more info
# changes current working directory for a block
# to the git working directory
#
# example
# @git.chdir do
# # write files
# @git.add
# @git.commit('message')
# end
def chdir(&block) # :yields: the Git::Path
chdir_block = Proc.new do
Dir.chdir(dir.path) do
block.call(dir.path)
end
end

if Process.respond_to?(:fork)
# Forking this process so that we can be threadsafe
pid = Process.fork do
chdir_block.call
end
Process.wait(pid)
else
# Windows and NetBSD 4 don't support fork()
# let's use a mutex to prevent race conditions with threads
Git::Base.chdir_semaphore.synchronize do
chdir_block.call
end
def chdir # :yields: the Git::Path
Dir.chdir(dir.path) do
yield dir.path
end
end

Expand Down Expand Up @@ -169,7 +144,9 @@ def repo

# returns the repository size in bytes
def repo_size
return `du -s #{repo.path}`.chomp.split.first.to_i
Dir.chdir(repo.path) do
return `du -s`.chomp.split.first.to_i
end
end

def set_index(index_file, check = true)
Expand Down
14 changes: 11 additions & 3 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def branches_all
def list_files(ref_dir)
dir = File.join(@git_dir, 'refs', ref_dir)
files = []
files = Dir.glob(File.join(dir, '**/*')).select { |f| File.file?(f) }
Dir.chdir(dir) { files = Dir.glob('**/*').select { |f| File.file?(f) } } rescue nil
files
end

Expand Down Expand Up @@ -445,7 +445,11 @@ def config_get(name)
command('config', ['--get', name])
end

do_get.call(@git_dir)
if @git_dir
Dir.chdir(@git_dir, &do_get)
else
do_get.call
end
end

def global_config_get(name)
Expand All @@ -457,7 +461,11 @@ def config_list
parse_config_list command_lines('config', ['--list'])
end

build_list.call(@git_dir)
if @git_dir
Dir.chdir(@git_dir, &build_list)
else
build_list.call
end
end

def global_config_list
Expand Down
16 changes: 8 additions & 8 deletions lib/git/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ def construct_status

def fetch_untracked
ignore = @base.lib.ignored_files
root_base_dir_path_length = File.join(@base.dir.path, '/').length
Dir.glob(File.join(@base.dir.path, '**/*'), File::FNM_DOTMATCH) do |file|
# Strip off the `base.dir.path` to make these relative paths
relative_file_name = file.slice(root_base_dir_path_length..file.length)
next if @files[relative_file_name] || File.directory?(file) ||
ignore.include?(relative_file_name) || relative_file_name =~ %r{^.git\/.+}

@files[relative_file_name] = { path: relative_file_name, untracked: true }

Dir.chdir(@base.dir.path) do
Dir.glob('**/*', File::FNM_DOTMATCH) do |file|
next if @files[file] || File.directory?(file) ||
ignore.include?(file) || file =~ %r{^.git\/.+}

@files[file] = { path: file, untracked: true }
end
end
end

Expand Down