Skip to content

Commit 3d789f7

Browse files
author
Joshua Liebowitz
committed
[WIP]Start replacing Dir.chdir with paths
Instead of using chdir blocks, we should use absolute paths so that we can enable threaded usage of the library Fixes #355
1 parent c8d1012 commit 3d789f7

File tree

4 files changed

+13
-23
lines changed

4 files changed

+13
-23
lines changed

lib/git.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def self.export(repository, name, options = {})
108108
options.delete(:remote)
109109
repo = clone(repository, name, {:depth => 1}.merge(options))
110110
repo.checkout("origin/#{options[:branch]}") if options[:branch]
111-
Dir.chdir(repo.dir.to_s) { FileUtils.rm_r '.git' }
111+
FileUtils.rm_r(File.join(repo.dir.to_s, '.git'))
112112
end
113113

114114
# Same as g.config, but forces it to be at the global level

lib/git/base.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,7 @@ def repo
144144

145145
# returns the repository size in bytes
146146
def repo_size
147-
Dir.chdir(repo.path) do
148-
return `du -s`.chomp.split.first.to_i
149-
end
147+
return `du -s #{repo.path}`.chomp.split.first.to_i
150148
end
151149

152150
def set_index(index_file, check = true)

lib/git/lib.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def branches_all
310310
def list_files(ref_dir)
311311
dir = File.join(@git_dir, 'refs', ref_dir)
312312
files = []
313-
Dir.chdir(dir) { files = Dir.glob('**/*').select { |f| File.file?(f) } } rescue nil
313+
files = Dir.glob(File.join(dir, '**/*')).select { |f| File.file?(f) }
314314
files
315315
end
316316

@@ -442,11 +442,7 @@ def config_get(name)
442442
command('config', ['--get', name])
443443
end
444444

445-
if @git_dir
446-
Dir.chdir(@git_dir, &do_get)
447-
else
448-
build_list.call
449-
end
445+
do_get.call(@git_dir)
450446
end
451447

452448
def global_config_get(name)
@@ -458,11 +454,7 @@ def config_list
458454
parse_config_list command_lines('config', ['--list'])
459455
end
460456

461-
if @git_dir
462-
Dir.chdir(@git_dir, &build_list)
463-
else
464-
build_list.call
465-
end
457+
build_list.call(@git_dir)
466458
end
467459

468460
def global_config_list

lib/git/status.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ def construct_status
171171

172172
def fetch_untracked
173173
ignore = @base.lib.ignored_files
174-
175-
Dir.chdir(@base.dir.path) do
176-
Dir.glob('**/*', File::FNM_DOTMATCH) do |file|
177-
next if @files[file] || File.directory?(file) ||
178-
ignore.include?(file) || file =~ %r{^.git\/.+}
179-
180-
@files[file] = { path: file, untracked: true }
181-
end
174+
root_base_dir_path_length = File.join(@base.dir.path, '/').length
175+
Dir.glob(File.join(@base.dir.path, '**/*'), File::FNM_DOTMATCH) do |file|
176+
# Strip off the `base.dir.path` to make these relative paths
177+
relative_file_name = file.slice(root_base_dir_path_length..file.length)
178+
next if @files[relative_file_name] || File.directory?(file) ||
179+
ignore.include?(relative_file_name) || relative_file_name =~ %r{^.git\/.+}
180+
181+
@files[relative_file_name] = { path: relative_file_name, untracked: true }
182182
end
183183
end
184184

0 commit comments

Comments
 (0)