From 7d3e8fa5ed9feb07a57e7fec685a46ccd58769ee Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Mon, 27 Jan 2025 16:26:12 -0300 Subject: [PATCH] Load bundled gems on expected version (#859) Fixes https://github.com/sds/overcommit/issues/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. --- template-dir/hooks/commit-msg | 10 ++-------- template-dir/hooks/overcommit-hook | 10 ++-------- template-dir/hooks/post-checkout | 10 ++-------- template-dir/hooks/post-commit | 10 ++-------- template-dir/hooks/post-merge | 10 ++-------- template-dir/hooks/post-rewrite | 10 ++-------- template-dir/hooks/pre-commit | 10 ++-------- template-dir/hooks/pre-push | 10 ++-------- template-dir/hooks/pre-rebase | 10 ++-------- template-dir/hooks/prepare-commit-msg | 10 ++-------- 10 files changed, 20 insertions(+), 80 deletions(-) diff --git a/template-dir/hooks/commit-msg b/template-dir/hooks/commit-msg index 7f8023de..377c892b 100755 --- a/template-dir/hooks/commit-msg +++ b/template-dir/hooks/commit-msg @@ -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 diff --git a/template-dir/hooks/overcommit-hook b/template-dir/hooks/overcommit-hook index 7f8023de..377c892b 100755 --- a/template-dir/hooks/overcommit-hook +++ b/template-dir/hooks/overcommit-hook @@ -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 diff --git a/template-dir/hooks/post-checkout b/template-dir/hooks/post-checkout index 7f8023de..377c892b 100755 --- a/template-dir/hooks/post-checkout +++ b/template-dir/hooks/post-checkout @@ -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 diff --git a/template-dir/hooks/post-commit b/template-dir/hooks/post-commit index 7f8023de..377c892b 100755 --- a/template-dir/hooks/post-commit +++ b/template-dir/hooks/post-commit @@ -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 diff --git a/template-dir/hooks/post-merge b/template-dir/hooks/post-merge index 7f8023de..377c892b 100755 --- a/template-dir/hooks/post-merge +++ b/template-dir/hooks/post-merge @@ -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 diff --git a/template-dir/hooks/post-rewrite b/template-dir/hooks/post-rewrite index 7f8023de..377c892b 100755 --- a/template-dir/hooks/post-rewrite +++ b/template-dir/hooks/post-rewrite @@ -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 diff --git a/template-dir/hooks/pre-commit b/template-dir/hooks/pre-commit index 7f8023de..377c892b 100755 --- a/template-dir/hooks/pre-commit +++ b/template-dir/hooks/pre-commit @@ -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 diff --git a/template-dir/hooks/pre-push b/template-dir/hooks/pre-push index 7f8023de..377c892b 100755 --- a/template-dir/hooks/pre-push +++ b/template-dir/hooks/pre-push @@ -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 diff --git a/template-dir/hooks/pre-rebase b/template-dir/hooks/pre-rebase index 7f8023de..377c892b 100755 --- a/template-dir/hooks/pre-rebase +++ b/template-dir/hooks/pre-rebase @@ -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 diff --git a/template-dir/hooks/prepare-commit-msg b/template-dir/hooks/prepare-commit-msg index 7f8023de..377c892b 100755 --- a/template-dir/hooks/prepare-commit-msg +++ b/template-dir/hooks/prepare-commit-msg @@ -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