Skip to content

Commit

Permalink
Load bundled gems on expected version (#859)
Browse files Browse the repository at this point in the history
Fixes #789

`psych` is no longer a default gem, but current and old ruby still ships
with it. When we `require 'yaml'`, we activate that gem in whatever
version that is bundled with ruby. Later on, we load bundler, and we
`Bundler.setup`, which will then activate whatever version specified in
the lock file. More often than not, they might not match.

The approach in this PR is to strip the `yaml` dependency completely
from the hook scripts, while retaining the ability to configure the
Gemfile using it.
  • Loading branch information
xjunior authored Jan 27, 2025
1 parent eff94a7 commit 7d3e8fa
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 80 deletions.
10 changes: 2 additions & 8 deletions template-dir/hooks/commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down
10 changes: 2 additions & 8 deletions template-dir/hooks/overcommit-hook
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down
10 changes: 2 additions & 8 deletions template-dir/hooks/post-checkout
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down
10 changes: 2 additions & 8 deletions template-dir/hooks/post-commit
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down
10 changes: 2 additions & 8 deletions template-dir/hooks/post-merge
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down
10 changes: 2 additions & 8 deletions template-dir/hooks/post-rewrite
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down
10 changes: 2 additions & 8 deletions template-dir/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down
10 changes: 2 additions & 8 deletions template-dir/hooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down
10 changes: 2 additions & 8 deletions template-dir/hooks/pre-rebase
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down
10 changes: 2 additions & 8 deletions template-dir/hooks/prepare-commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ if hook_type == 'overcommit-hook'
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
gemfile =
begin
YAML.load_file('.overcommit.yml', aliases: true)['gemfile']
rescue ArgumentError
YAML.load_file('.overcommit.yml')['gemfile']
end rescue nil
config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/
gemfile = Regexp.last_match(1)

if gemfile
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down

0 comments on commit 7d3e8fa

Please sign in to comment.