Skip to content

Commit

Permalink
Remove usage of Dir.chdir that just execute a subprocess
Browse files Browse the repository at this point in the history
Preferring instead to spawn the subprocess in the correct directory
  • Loading branch information
segiddins committed Aug 31, 2023
1 parent 54032f3 commit 7f56780
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bundler/lib/bundler/build_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.git_commit_sha
# commit instance variable then we can't determine its commits SHA.
git_dir = File.expand_path("../../../.git", __dir__)
if File.directory?(git_dir)
return @git_commit_sha = Dir.chdir(git_dir) { `git rev-parse --short HEAD`.strip.freeze }
return @git_commit_sha = IO.popen(%w[git rev-parse --short HEAD], { :chdir => git_dir }, &:read).strip.freeze
end

@git_commit_sha ||= "unknown"
Expand Down
4 changes: 1 addition & 3 deletions bundler/lib/bundler/cli/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,7 @@ def run
end

if use_git
Dir.chdir(target) do
`git add .`
end
IO.popen(%w[git add .], { :chdir => target }, &:read)
end

# Open gemspec in editor
Expand Down
12 changes: 5 additions & 7 deletions bundler/lib/bundler/cli/open.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ def run
Bundler.ui.info "Unable to open #{name} because it's a default gem, so the directory it would normally be installed to does not exist."
else
root_path = spec.full_gem_path
Dir.chdir(root_path) do
require "shellwords"
command = Shellwords.split(editor) << File.join([root_path, path].compact)
Bundler.with_original_env do
system(*command)
end || Bundler.ui.info("Could not run '#{command.join(" ")}'")
end
require "shellwords"
command = Shellwords.split(editor) << File.join([root_path, path].compact)
Bundler.with_original_env do
system(*command, { :chdir => root_path })
end || Bundler.ui.info("Could not run '#{command.join(" ")}'")
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/rubygems/commands/open_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def open_gem(name)
end

def open_editor(path)
Dir.chdir(path) do
system(*@editor.split(/\s+/) + [path])
end
system(*@editor.split(/\s+/) + [path], { :chdir => path })
end

def spec_for(name)
Expand Down

0 comments on commit 7f56780

Please sign in to comment.