From 26e78aa9e5e907ed41ebdac8098483af4d54865c Mon Sep 17 00:00:00 2001 From: fynsta <63241108+fynsta@users.noreply.github.com> Date: Mon, 24 Jun 2024 20:54:52 +0200 Subject: [PATCH 01/16] Add changelog_uri to gemspec (#851) Supported here: https://guides.rubygems.org/specification-reference/#metadata Useful for running https://github.com/MaximeD/gem_updater --- overcommit.gemspec | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/overcommit.gemspec b/overcommit.gemspec index 268a9fc1..0b3a590d 100644 --- a/overcommit.gemspec +++ b/overcommit.gemspec @@ -15,6 +15,10 @@ Gem::Specification.new do |s| s.post_install_message = 'Install hooks by running `overcommit --install` in your Git repository' + s.metadata = { + 'changelog_uri' => 'https://github.com/sds/overcommit/blob/main/CHANGELOG.md' + } + s.require_paths = %w[lib] s.executables = ['overcommit'] From 939d9e6be210fb59b453e3ce18abf4409d105503 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Mon, 22 Jul 2024 13:48:43 -0700 Subject: [PATCH 02/16] Cut version 0.64.0 (#852) --- lib/overcommit/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/overcommit/version.rb b/lib/overcommit/version.rb index e640bf38..9df380b0 100644 --- a/lib/overcommit/version.rb +++ b/lib/overcommit/version.rb @@ -2,5 +2,5 @@ # Defines the gem version. module Overcommit - VERSION = '0.63.0' + VERSION = '0.64.0' end From 5ff9d5e78906813fc9421f2ce9388aee9363b8bf Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Mon, 22 Jul 2024 13:52:53 -0700 Subject: [PATCH 03/16] Update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f61b7d5c..d8d2133f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Overcommit Changelog +## 0.64.0 + +* Add support for `stylelint` 16+ +* Add `changelog_uri` to gemspec + ## 0.63.0 * Add `Sorbet` pre-commit hook From 5a3d68eaa70573bb600bb9deedda0e5d622eafea Mon Sep 17 00:00:00 2001 From: Joakim Antman Date: Sun, 11 Aug 2024 09:31:18 +0300 Subject: [PATCH 04/16] Fix spec compatibility with recent git versions (#854) The original array was defining the shellwords like this: `["git", "commit", "-m", "\"Resolve", "conflicts\"", "-i", "some-file"]` This combined with the most recent git version failed with: ``` error: pathspec 'conflicts"' did not match any file(s) known to git ``` This PR just simplifies the commit message to work with the %w array literal. --- spec/integration/resolving_cherry_pick_conflict_spec.rb | 2 +- spec/integration/resolving_merge_conflict_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/integration/resolving_cherry_pick_conflict_spec.rb b/spec/integration/resolving_cherry_pick_conflict_spec.rb index 58e22e65..2a7f053c 100644 --- a/spec/integration/resolving_cherry_pick_conflict_spec.rb +++ b/spec/integration/resolving_cherry_pick_conflict_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'resolving cherry-pick conflicts' do - subject { shell(%w[git commit -m "Resolve conflicts" -i some-file]) } + subject { shell(%w[git commit -m Test -i some-file]) } let(:config) { <<-YML } PreCommit: diff --git a/spec/integration/resolving_merge_conflict_spec.rb b/spec/integration/resolving_merge_conflict_spec.rb index 679ba136..997dcb09 100644 --- a/spec/integration/resolving_merge_conflict_spec.rb +++ b/spec/integration/resolving_merge_conflict_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'resolving merge conflicts' do - subject { shell(%w[git commit -m "Resolve conflicts" -i some-file]) } + subject { shell(%w[git commit -m Test -i some-file]) } around do |example| repo do From 31c83ce603008de3ac04ff9b7d1ea9fca6e529c8 Mon Sep 17 00:00:00 2001 From: Joakim Antman Date: Sun, 11 Aug 2024 09:37:24 +0300 Subject: [PATCH 05/16] Expand tildes(~) for hooksPath (#853) While installing overcommit git hooks i noticed that tildes are not expanded to home folders for the `hooksPath` config, the result was a tilde folder in the current directory. This PR addresses that by switching `File.absolute_path` to `File.expand_path`. The [underlaying implementation](https://github.com/ruby/ruby/blob/v3_3_4/file.c#L3753) in Ruby is exactly the same just with this difference in how `~` is handled. --------- Co-authored-by: Shane da Silva --- lib/overcommit/git_config.rb | 2 +- spec/overcommit/git_config_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/overcommit/git_config.rb b/lib/overcommit/git_config.rb index 392dd9da..c1243861 100644 --- a/lib/overcommit/git_config.rb +++ b/lib/overcommit/git_config.rb @@ -17,7 +17,7 @@ def hooks_path path = `git config --get core.hooksPath`.chomp return File.join(Overcommit::Utils.git_dir, 'hooks') if path.empty? - File.absolute_path(path, Dir.pwd) + File.expand_path(path, Dir.pwd) end end end diff --git a/spec/overcommit/git_config_spec.rb b/spec/overcommit/git_config_spec.rb index 22ad7c02..9cc51862 100644 --- a/spec/overcommit/git_config_spec.rb +++ b/spec/overcommit/git_config_spec.rb @@ -78,5 +78,19 @@ expect(subject).to eq File.expand_path('my-hooks') end end + + context 'when explicitly set to a path starting with a tilde' do + around do |example| + repo do + `git config --local core.hooksPath ~/my-hooks` + example.run + end + end + + it 'returns the absolute path to the folder in the users home path' do + expect(subject).to eq File.expand_path('~/my-hooks') + expect(subject).not_to include('~') + end + end end end From 9825868ba2b2194a2329d24c08d47a054bf265cc Mon Sep 17 00:00:00 2001 From: Edward Woodcock <768254+RemoteCTO@users.noreply.github.com> Date: Thu, 31 Oct 2024 05:47:17 +0100 Subject: [PATCH 06/16] Bump rexml to >= 3.3.9 to resolve GHSA-2rxp-v6pw-ch6m (#857) A `ReDoS vulnerability in REXML` has been identified in versions <3.3.9 Details in GitHub: - https://github.com/ruby/rexml/security/advisories/GHSA-2rxp-v6pw-ch6m This is a small bump to the latest patched version. This should resolve anybody getting the following `bundle audit` error when using overcommit: ``` Name: rexml Version: 3.3.8 CVE: CVE-2024-49761 GHSA: GHSA-2rxp-v6pw-ch6m Criticality: High URL: https://github.com/ruby/rexml/security/advisories/GHSA-2rxp-v6pw-ch6m Title: REXML ReDoS vulnerability Solution: update to '>= 3.3.9' ``` --- lib/overcommit/version.rb | 2 +- overcommit.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/overcommit/version.rb b/lib/overcommit/version.rb index 9df380b0..afd18470 100644 --- a/lib/overcommit/version.rb +++ b/lib/overcommit/version.rb @@ -2,5 +2,5 @@ # Defines the gem version. module Overcommit - VERSION = '0.64.0' + VERSION = '0.64.1' end diff --git a/overcommit.gemspec b/overcommit.gemspec index 0b3a590d..caaa8499 100644 --- a/overcommit.gemspec +++ b/overcommit.gemspec @@ -33,5 +33,5 @@ Gem::Specification.new do |s| s.add_dependency 'childprocess', '>= 0.6.3', '< 6' s.add_dependency 'iniparse', '~> 1.4' - s.add_dependency 'rexml', '~> 3.2' + s.add_dependency 'rexml', '>= 3.3.9' end From eff94a7f058d9eeca3f30e069fadba5a82f43da2 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Wed, 30 Oct 2024 21:49:26 -0700 Subject: [PATCH 07/16] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8d2133f..1ba199d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Overcommit Changelog +## 0.64.1 + +* Update minimum version of rexml to address [CVE-2024-49761](https://www.ruby-lang.org/en/news/2024/10/28/redos-rexml-cve-2024-49761/) + ## 0.64.0 * Add support for `stylelint` 16+ From 7d3e8fa5ed9feb07a57e7fec685a46ccd58769ee Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Mon, 27 Jan 2025 16:26:12 -0300 Subject: [PATCH 08/16] 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 From 9ce54927dda3fdc7de00fbfcf68a9ebaf9e008c3 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Mon, 27 Jan 2025 11:27:56 -0800 Subject: [PATCH 09/16] Cut version 0.65.0 --- CHANGELOG.md | 4 ++++ lib/overcommit/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ba199d2..bbe08fb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Overcommit Changelog +## 0.65.0 + +* Load bundled gems on expected version + ## 0.64.1 * Update minimum version of rexml to address [CVE-2024-49761](https://www.ruby-lang.org/en/news/2024/10/28/redos-rexml-cve-2024-49761/) diff --git a/lib/overcommit/version.rb b/lib/overcommit/version.rb index afd18470..0b718a39 100644 --- a/lib/overcommit/version.rb +++ b/lib/overcommit/version.rb @@ -2,5 +2,5 @@ # Defines the gem version. module Overcommit - VERSION = '0.64.1' + VERSION = '0.65.0' end From b4d4ce0e819dad926dfd0073540dd73a5217e2e9 Mon Sep 17 00:00:00 2001 From: benmelz Date: Wed, 29 Jan 2025 19:26:01 -0500 Subject: [PATCH 10/16] Add `-diff` cli option for running precommit hooks against diffs (#860) For example, running `overcommit --diff main` from a feature branch will run pre-commit hooks against the diff between the two branches. I was able to very easily leverage existing code for the bulk of the feature - this is mainly just adding the cli option, a hook context to do the execution and some tests based on the existing `--run-all` test. --- For background, my team is responsible for a couple of really old, really large rails apps. Getting them completely in compliance with our various linters is a huge task that isn't getting done anytime soon (things are funky to the point that we've even observed breakages with "safe" auto-correct functions). I introduced/started heavily encouraging overcommit so that we at least don't add _new_ linting offenses and things will naturally improve over time. It's been great, but offenses still slip through though here and there, especially with juniors who might be getting away with not having a local install (and/or abusing `OVERCOMMIT_DISABLE=1`). An option like this would allow me to leverage the very useful "only apply to changed lines" logic within a ci environment and help enforce my desired "no new linting offenses" policy. --- lib/overcommit/cli.rb | 23 +++- lib/overcommit/hook_context.rb | 4 +- lib/overcommit/hook_context/base.rb | 4 +- lib/overcommit/hook_context/diff.rb | 37 ++++++ spec/integration/diff_flag_spec.rb | 39 ++++++ spec/overcommit/cli_spec.rb | 26 ++++ spec/overcommit/hook_context/diff_spec.rb | 143 ++++++++++++++++++++++ 7 files changed, 272 insertions(+), 4 deletions(-) create mode 100644 lib/overcommit/hook_context/diff.rb create mode 100644 spec/integration/diff_flag_spec.rb create mode 100644 spec/overcommit/hook_context/diff_spec.rb diff --git a/lib/overcommit/cli.rb b/lib/overcommit/cli.rb index f8127125..dafc545a 100644 --- a/lib/overcommit/cli.rb +++ b/lib/overcommit/cli.rb @@ -9,6 +9,7 @@ module Overcommit class CLI # rubocop:disable Metrics/ClassLength def initialize(arguments, input, logger) @arguments = arguments + @cli_options = {} @input = input @log = logger @options = {} @@ -28,6 +29,8 @@ def run sign when :run_all run_all + when :diff + diff end rescue Overcommit::Exceptions::ConfigurationSignatureChanged => e puts e @@ -45,7 +48,7 @@ def parse_arguments @parser = create_option_parser begin - @parser.parse!(@arguments) + @parser.parse!(@arguments, into: @cli_options) # Default action is to install @options[:action] ||= :install @@ -98,6 +101,11 @@ def add_installation_options(opts) @options[:action] = :run_all @options[:hook_to_run] = arg ? arg.to_s : 'run-all' end + + opts.on('--diff [ref]', 'Run pre_commit hooks against the diff between a given ref. Defaults to `main`.') do |arg| # rubocop:disable Layout/LineLength + @options[:action] = :diff + arg + end end def add_other_options(opts) @@ -209,6 +217,19 @@ def run_all halt(status ? 0 : 65) end + def diff + empty_stdin = File.open(File::NULL) # pre-commit hooks don't take input + context = Overcommit::HookContext.create('diff', config, @arguments, empty_stdin, **@cli_options) # rubocop:disable Layout/LineLength + config.apply_environment!(context, ENV) + + printer = Overcommit::Printer.new(config, log, context) + runner = Overcommit::HookRunner.new(config, log, context, printer) + + status = runner.run + + halt(status ? 0 : 65) + end + # Used for ease of stubbing in tests def halt(status = 0) exit status diff --git a/lib/overcommit/hook_context.rb b/lib/overcommit/hook_context.rb index acdff1bb..5863ed94 100644 --- a/lib/overcommit/hook_context.rb +++ b/lib/overcommit/hook_context.rb @@ -2,13 +2,13 @@ # Utility module which manages the creation of {HookContext}s. module Overcommit::HookContext - def self.create(hook_type, config, args, input) + def self.create(hook_type, config, args, input, **cli_options) hook_type_class = Overcommit::Utils.camel_case(hook_type) underscored_hook_type = Overcommit::Utils.snake_case(hook_type) require "overcommit/hook_context/#{underscored_hook_type}" - Overcommit::HookContext.const_get(hook_type_class).new(config, args, input) + Overcommit::HookContext.const_get(hook_type_class).new(config, args, input, **cli_options) rescue LoadError, NameError => e # Could happen when a symlink was created for a hook type Overcommit does # not yet support. diff --git a/lib/overcommit/hook_context/base.rb b/lib/overcommit/hook_context/base.rb index 077394fb..b50698c9 100644 --- a/lib/overcommit/hook_context/base.rb +++ b/lib/overcommit/hook_context/base.rb @@ -18,10 +18,12 @@ class Base # @param config [Overcommit::Configuration] # @param args [Array] # @param input [IO] standard input stream - def initialize(config, args, input) + # @param options [Hash] cli options + def initialize(config, args, input, **options) @config = config @args = args @input = input + @options = options end # Executes a command as if it were a regular git hook, passing all diff --git a/lib/overcommit/hook_context/diff.rb b/lib/overcommit/hook_context/diff.rb new file mode 100644 index 00000000..0c4b97e0 --- /dev/null +++ b/lib/overcommit/hook_context/diff.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'overcommit/git_repo' + +module Overcommit::HookContext + # Simulates a pre-commit context based on the diff with another git ref. + # + # This results in pre-commit hooks running against the changes between the current + # and another ref, which is useful for automated CI scripts. + class Diff < Base + def modified_files + @modified_files ||= Overcommit::GitRepo.modified_files(refs: @options[:diff]) + end + + def modified_lines_in_file(file) + @modified_lines ||= {} + @modified_lines[file] ||= Overcommit::GitRepo.extract_modified_lines(file, + refs: @options[:diff]) + end + + def hook_class_name + 'PreCommit' + end + + def hook_type_name + 'pre_commit' + end + + def hook_script_name + 'pre-commit' + end + + def initial_commit? + @initial_commit ||= Overcommit::GitRepo.initial_commit? + end + end +end diff --git a/spec/integration/diff_flag_spec.rb b/spec/integration/diff_flag_spec.rb new file mode 100644 index 00000000..dcaa1bd3 --- /dev/null +++ b/spec/integration/diff_flag_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'overcommit --diff' do + subject { shell(%w[overcommit --diff main]) } + + context 'when using an existing pre-commit hook script' do + let(:script_name) { 'test-script' } + let(:script_contents) { "#!/bin/bash\nexit 0" } + let(:script_path) { ".#{Overcommit::OS::SEPARATOR}#{script_name}" } + + let(:config) do + { + 'PreCommit' => { + 'MyHook' => { + 'enabled' => true, + 'required_executable' => script_path, + } + } + } + end + + around do |example| + repo do + File.open('.overcommit.yml', 'w') { |f| f.puts(config.to_yaml) } + echo(script_contents, script_path) + `git add #{script_path}` + FileUtils.chmod(0o755, script_path) + example.run + end + end + + it 'completes successfully without blocking' do + wait_until(timeout: 10) { subject } # Need to wait long time for JRuby startup + subject.status.should == 0 + end + end +end diff --git a/spec/overcommit/cli_spec.rb b/spec/overcommit/cli_spec.rb index 40923ad3..3d95b09d 100644 --- a/spec/overcommit/cli_spec.rb +++ b/spec/overcommit/cli_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' require 'overcommit/cli' +require 'overcommit/hook_context/diff' require 'overcommit/hook_context/run_all' describe Overcommit::CLI do @@ -125,5 +126,30 @@ subject end end + + context 'with the diff switch specified' do + let(:arguments) { ['--diff=some-ref'] } + let(:config) { Overcommit::ConfigurationLoader.default_configuration } + + before do + cli.stub(:halt) + Overcommit::HookRunner.any_instance.stub(:run) + end + + it 'creates a HookRunner with the diff context' do + Overcommit::HookRunner.should_receive(:new). + with(config, + logger, + instance_of(Overcommit::HookContext::Diff), + instance_of(Overcommit::Printer)). + and_call_original + subject + end + + it 'runs the HookRunner' do + Overcommit::HookRunner.any_instance.should_receive(:run) + subject + end + end end end diff --git a/spec/overcommit/hook_context/diff_spec.rb b/spec/overcommit/hook_context/diff_spec.rb new file mode 100644 index 00000000..0c712fbc --- /dev/null +++ b/spec/overcommit/hook_context/diff_spec.rb @@ -0,0 +1,143 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'overcommit/hook_context/diff' + +describe Overcommit::HookContext::Diff do + let(:config) { double('config') } + let(:args) { [] } + let(:input) { double('input') } + let(:context) { described_class.new(config, args, input, diff: 'master') } + + describe '#modified_files' do + subject { context.modified_files } + + context 'when repo contains no files' do + around do |example| + repo do + `git commit --allow-empty -m "Initial commit"` + `git checkout -b other-branch 2>&1` + example.run + end + end + + it { should be_empty } + end + + context 'when the repo contains files that are unchanged from the ref' do + around do |example| + repo do + touch('some-file') + `git add some-file` + touch('some-other-file') + `git add some-other-file` + `git commit -m "Add files"` + `git checkout -b other-branch 2>&1` + example.run + end + end + + it { should be_empty } + end + + context 'when repo contains files that have been changed from the ref' do + around do |example| + repo do + touch('some-file') + `git add some-file` + touch('some-other-file') + `git add some-other-file` + `git commit -m "Add files"` + `git checkout -b other-branch 2>&1` + File.open('some-file', 'w') { |f| f.write("hello\n") } + `git add some-file` + `git commit -m "Edit file"` + example.run + end + end + + it { should == %w[some-file].map { |file| File.expand_path(file) } } + end + + context 'when repo contains submodules' do + around do |example| + submodule = repo do + touch 'foo' + `git add foo` + `git commit -m "Initial commit"` + end + + repo do + `git submodule add #{submodule} test-sub 2>&1 > #{File::NULL}` + `git commit --allow-empty -m "Initial commit"` + `git checkout -b other-branch 2>&1` + example.run + end + end + + it { should_not include File.expand_path('test-sub') } + end + end + + describe '#modified_lines_in_file' do + let(:modified_file) { 'some-file' } + subject { context.modified_lines_in_file(modified_file) } + + context 'when file contains a trailing newline' do + around do |example| + repo do + touch(modified_file) + `git add #{modified_file}` + `git commit -m "Add file"` + `git checkout -b other-branch 2>&1` + File.open(modified_file, 'w') { |f| (1..3).each { |i| f.write("#{i}\n") } } + `git add #{modified_file}` + `git commit -m "Edit file"` + example.run + end + end + + it { should == Set.new(1..3) } + end + + context 'when file does not contain a trailing newline' do + around do |example| + repo do + touch(modified_file) + `git add #{modified_file}` + `git commit -m "Add file"` + `git checkout -b other-branch 2>&1` + File.open(modified_file, 'w') do |f| + (1..2).each { |i| f.write("#{i}\n") } + f.write(3) + end + `git add #{modified_file}` + `git commit -m "Edit file"` + example.run + end + end + + it { should == Set.new(1..3) } + end + end + + describe '#hook_type_name' do + subject { context.hook_type_name } + + it { should == 'pre_commit' } + end + + describe '#hook_script_name' do + subject { context.hook_script_name } + + it { should == 'pre-commit' } + end + + describe '#initial_commit?' do + subject { context.initial_commit? } + + before { Overcommit::GitRepo.stub(:initial_commit?).and_return(true) } + + it { should == true } + end +end From 43e17fb384e51101cd45449e03d39188f18959f9 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Wed, 29 Jan 2025 16:32:23 -0800 Subject: [PATCH 11/16] Cut version 0.66.0 (#861) --- CHANGELOG.md | 4 ++++ README.md | 1 + lib/overcommit/version.rb | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbe08fb9..d82b5b50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Overcommit Changelog +## 0.66.0 + +* Add `--diff` CLI option for running pre-commit hooks against only changed files + ## 0.65.0 * Load bundled gems on expected version diff --git a/README.md b/README.md index 877e9396..a0d46726 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ Command Line Flag | Description `-f`/`--force` | Don't bail on install if other hooks already exist--overwrite them `-l`/`--list-hooks` | Display all available hooks in the current repository `-r`/`--run` | Run pre-commit hook against all tracked files in repository +`--diff ` | Run pre-commit hook against all changed files relative to `` `-t`/`--template-dir` | Print location of template directory `-h`/`--help` | Show command-line flag documentation `-v`/`--version` | Show version diff --git a/lib/overcommit/version.rb b/lib/overcommit/version.rb index 0b718a39..c1091e52 100644 --- a/lib/overcommit/version.rb +++ b/lib/overcommit/version.rb @@ -2,5 +2,5 @@ # Defines the gem version. module Overcommit - VERSION = '0.65.0' + VERSION = '0.66.0' end From 3db733e5b7dd479baee1c8e587aa86ed73e5011f Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Sun, 16 Feb 2025 13:04:54 -0800 Subject: [PATCH 12/16] Restore ability to specify "gemfile: false" in config (#863) Setting `gemfile: false` in `.overcommit.yml` is supposed to disable Bundler. However, a recently-introduced bug causes `false` to be interpreted as the name of the gemfile. Bundler looks for a gemfile named "false", which fails, leading overcommit's hooks to crash. This PR fixes the bug by adjusting the regex used to parse the `gemfile:` line in the config. Now, `false` is no longer interpreted as a gemfile name. I added an integration test to verify the fix. Fixes #862 --- spec/integration/gemfile_option_spec.rb | 211 +++++++++++++++--------- template-dir/hooks/commit-msg | 2 +- template-dir/hooks/overcommit-hook | 2 +- template-dir/hooks/post-checkout | 2 +- template-dir/hooks/post-commit | 2 +- template-dir/hooks/post-merge | 2 +- template-dir/hooks/post-rewrite | 2 +- template-dir/hooks/pre-commit | 2 +- template-dir/hooks/pre-push | 2 +- template-dir/hooks/pre-rebase | 2 +- template-dir/hooks/prepare-commit-msg | 2 +- 11 files changed, 144 insertions(+), 87 deletions(-) diff --git a/spec/integration/gemfile_option_spec.rb b/spec/integration/gemfile_option_spec.rb index 7a0175da..a6a6a4ba 100644 --- a/spec/integration/gemfile_option_spec.rb +++ b/spec/integration/gemfile_option_spec.rb @@ -3,99 +3,156 @@ require 'spec_helper' describe 'specifying `gemfile` option in Overcommit configuration' do - let(:repo_root) { File.expand_path(File.join('..', '..'), File.dirname(__FILE__)) } - let(:fake_gem_path) { File.join('lib', 'my_fake_gem') } - - # We point the overcommit gem back to this repo since we can't assume the gem - # has already been installed in a test environment - let(:gemfile) { normalize_indent(<<-RUBY) } - source 'https://rubygems.org' - - gem 'overcommit', path: '#{repo_root}' - gem 'my_fake_gem', path: '#{fake_gem_path}' - gem 'ffi' if Gem.win_platform? # Necessary for test to pass on Windows - RUBY - - let(:gemspec) { normalize_indent(<<-RUBY) } - Gem::Specification.new do |s| - s.name = 'my_fake_gem' - s.version = '1.0.0' - s.author = 'John Doe' - s.license = 'MIT' - s.homepage = 'https://example.com' - s.email = 'john.doe@example.com' - s.summary = 'A fake gem' - s.files = [File.join('lib', 'my_fake_gem.rb')] - end - RUBY - - # Specify a hook that depends on an external gem to test Gemfile loading - let(:hook) { normalize_indent(<<-RUBY) } - module Overcommit::Hook::PreCommit - class FakeHook < Base - def run - require 'my_fake_gem' - :pass + context 'given a project that uses a Gemfile' do + let(:repo_root) { File.expand_path(File.join('..', '..'), File.dirname(__FILE__)) } + let(:fake_gem_path) { File.join('lib', 'my_fake_gem') } + + # We point the overcommit gem back to this repo since we can't assume the gem + # has already been installed in a test environment + let(:gemfile) { normalize_indent(<<-RUBY) } + source 'https://rubygems.org' + + gem 'overcommit', path: '#{repo_root}' + gem 'my_fake_gem', path: '#{fake_gem_path}' + gem 'ffi' if Gem.win_platform? # Necessary for test to pass on Windows + RUBY + + let(:gemspec) { normalize_indent(<<-RUBY) } + Gem::Specification.new do |s| + s.name = 'my_fake_gem' + s.version = '1.0.0' + s.author = 'John Doe' + s.license = 'MIT' + s.homepage = 'https://example.com' + s.email = 'john.doe@example.com' + s.summary = 'A fake gem' + s.files = [File.join('lib', 'my_fake_gem.rb')] + end + RUBY + + # Specify a hook that depends on an external gem to test Gemfile loading + let(:hook) { normalize_indent(<<-RUBY) } + module Overcommit::Hook::PreCommit + class FakeHook < Base + def run + require 'my_fake_gem' + :pass + end + end + end + RUBY + + let(:config) { normalize_indent(<<-YAML) } + verify_signatures: false + + CommitMsg: + ALL: + enabled: false + + PreCommit: + ALL: + enabled: false + FakeHook: + enabled: true + requires_files: false + YAML + + around do |example| + repo do + # Since RSpec is being run within a Bundler context we need to clear it + # in order to not taint the test + Bundler.with_unbundled_env do + FileUtils.mkdir_p(File.join(fake_gem_path, 'lib')) + echo(gemspec, File.join(fake_gem_path, 'my_fake_gem.gemspec')) + touch(File.join(fake_gem_path, 'lib', 'my_fake_gem.rb')) + + echo(gemfile, '.overcommit_gems.rb') + `bundle install --gemfile=.overcommit_gems.rb` + + echo(config, '.overcommit.yml') + + # Set BUNDLE_GEMFILE so we load Overcommit from the current repo + ENV['BUNDLE_GEMFILE'] = '.overcommit_gems.rb' + `bundle exec overcommit --install > #{File::NULL}` + FileUtils.mkdir_p(File.join('.git-hooks', 'pre_commit')) + echo(hook, File.join('.git-hooks', 'pre_commit', 'fake_hook.rb')) + + Overcommit::Utils.with_environment 'OVERCOMMIT_NO_VERIFY' => '1' do + example.run + end end end end - RUBY - - let(:config) { normalize_indent(<<-YAML) } - verify_signatures: false - - CommitMsg: - ALL: - enabled: false - - PreCommit: - ALL: - enabled: false - FakeHook: - enabled: true - requires_files: false - YAML - - around do |example| - repo do - # Since RSpec is being run within a Bundler context we need to clear it - # in order to not taint the test - Bundler.with_unbundled_env do - FileUtils.mkdir_p(File.join(fake_gem_path, 'lib')) - echo(gemspec, File.join(fake_gem_path, 'my_fake_gem.gemspec')) - touch(File.join(fake_gem_path, 'lib', 'my_fake_gem.rb')) - - echo(gemfile, '.overcommit_gems.rb') - `bundle install --gemfile=.overcommit_gems.rb` + subject { shell(%w[git commit --allow-empty -m Test]) } + + context 'when configuration specifies the gemfile' do + let(:config) { "gemfile: .overcommit_gems.rb\n" + super() } + + it 'runs the hook successfully' do + subject.status.should == 0 + end + end + + context 'when configuration does not specify the gemfile' do + it 'fails to run the hook' do + subject.status.should_not == 0 + end + end + end + + context 'given a project that does not use a Gemfile' do + let(:hook) { normalize_indent(<<-RUBY) } + module Overcommit::Hook::PreCommit + class NoInvalidGemfileHook < Base + def run + if (gemfile = ENV["BUNDLE_GEMFILE"]) + raise unless File.exist?(gemfile) + end + + :pass + end + end + end + RUBY + + let(:config) { normalize_indent(<<-YAML) } + verify_signatures: false + + CommitMsg: + ALL: + enabled: false + + PreCommit: + ALL: + enabled: false + NoInvalidGemfileHook: + enabled: true + requires_files: false + YAML + + around do |example| + repo do echo(config, '.overcommit.yml') - # Set BUNDLE_GEMFILE so we load Overcommit from the current repo - ENV['BUNDLE_GEMFILE'] = '.overcommit_gems.rb' - `bundle exec overcommit --install > #{File::NULL}` + `overcommit --install > #{File::NULL}` FileUtils.mkdir_p(File.join('.git-hooks', 'pre_commit')) - echo(hook, File.join('.git-hooks', 'pre_commit', 'fake_hook.rb')) + echo(hook, File.join('.git-hooks', 'pre_commit', 'no_invalid_gemfile_hook.rb')) Overcommit::Utils.with_environment 'OVERCOMMIT_NO_VERIFY' => '1' do example.run end end end - end - - subject { shell(%w[git commit --allow-empty -m Test]) } - context 'when configuration specifies the gemfile' do - let(:config) { "gemfile: .overcommit_gems.rb\n" + super() } + subject { shell(%w[git commit --allow-empty -m Test]) } - it 'runs the hook successfully' do - subject.status.should == 0 - end - end + context 'when configuration explicitly sets the gemfile to false' do + let(:config) { "gemfile: false\n" + super() } - context 'when configuration does not specify the gemfile' do - it 'fails to run the hook' do - subject.status.should_not == 0 + it 'runs the hook successfully' do + subject.status.should == 0 + end end end end diff --git a/template-dir/hooks/commit-msg b/template-dir/hooks/commit-msg index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/commit-msg +++ b/template-dir/hooks/commit-msg @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile diff --git a/template-dir/hooks/overcommit-hook b/template-dir/hooks/overcommit-hook index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/overcommit-hook +++ b/template-dir/hooks/overcommit-hook @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile diff --git a/template-dir/hooks/post-checkout b/template-dir/hooks/post-checkout index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/post-checkout +++ b/template-dir/hooks/post-checkout @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile diff --git a/template-dir/hooks/post-commit b/template-dir/hooks/post-commit index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/post-commit +++ b/template-dir/hooks/post-commit @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile diff --git a/template-dir/hooks/post-merge b/template-dir/hooks/post-merge index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/post-merge +++ b/template-dir/hooks/post-merge @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile diff --git a/template-dir/hooks/post-rewrite b/template-dir/hooks/post-rewrite index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/post-rewrite +++ b/template-dir/hooks/post-rewrite @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile diff --git a/template-dir/hooks/pre-commit b/template-dir/hooks/pre-commit index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/pre-commit +++ b/template-dir/hooks/pre-commit @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile diff --git a/template-dir/hooks/pre-push b/template-dir/hooks/pre-push index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/pre-push +++ b/template-dir/hooks/pre-push @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile diff --git a/template-dir/hooks/pre-rebase b/template-dir/hooks/pre-rebase index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/pre-rebase +++ b/template-dir/hooks/pre-rebase @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile diff --git a/template-dir/hooks/prepare-commit-msg b/template-dir/hooks/prepare-commit-msg index 377c892b..87ffeb58 100755 --- a/template-dir/hooks/prepare-commit-msg +++ b/template-dir/hooks/prepare-commit-msg @@ -27,7 +27,7 @@ if hook_type == 'overcommit-hook' end # Check if Overcommit should invoke a Bundler context for loading gems -config = File.read('.overcommit.yml') =~ /gemfile: ['"]?(.*)['"]?/ +File.read('.overcommit.yml') =~ /gemfile: (?:false|['"]?(.*)['"]?)/ gemfile = Regexp.last_match(1) if gemfile From 88bee0845bff4142642253f89aabaa9fd318465b Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Sun, 16 Feb 2025 13:08:25 -0800 Subject: [PATCH 13/16] Cut version 0.67.0 (#864) --- CHANGELOG.md | 4 ++++ lib/overcommit/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d82b5b50..8b99417a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Overcommit Changelog +## 0.67.0 + +* Fix bug introduced in 0.65.0 that prevented `gemfile: false` from working correctly + ## 0.66.0 * Add `--diff` CLI option for running pre-commit hooks against only changed files diff --git a/lib/overcommit/version.rb b/lib/overcommit/version.rb index c1091e52..803dbbfc 100644 --- a/lib/overcommit/version.rb +++ b/lib/overcommit/version.rb @@ -2,5 +2,5 @@ # Defines the gem version. module Overcommit - VERSION = '0.66.0' + VERSION = '0.67.0' end From 9f142c2a4f66a63e27ae2e5c64f507fc36f3807b Mon Sep 17 00:00:00 2001 From: Pedro Fayolle Date: Thu, 20 Feb 2025 03:17:00 +0900 Subject: [PATCH 14/16] Add note about unsupported YAML features in `gemfile:` line in default.yml (#865) Related to #863 and [this comment](https://github.com/sds/overcommit/issues/862#issuecomment-2641924147): > Having a similar problem since we had this line in our `.overcommit.yml`: > > ```yaml > gemfile: Gemfile # enforce bundled version of overcommit > ``` > > And now overcommit doesn't strip out the inline comment, resulting in this weird looking error message: > > ``` > Problem loading 'Gemfile # enforce bundled version of overcommit': /path/to/project/Gemfile # enforce bundled version of overcommit not found > ``` I think adding support for comments in the `gemfile:` regexp is likely overkill and may still not be enough when the next person tries to use yet another YAML feature in that line, but perhaps this little warning would help someone else avoid tripping. --- config/default.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/default.yml b/config/default.yml index 70ae0b2d..ba8af533 100644 --- a/config/default.yml +++ b/config/default.yml @@ -32,6 +32,10 @@ # your repository, and then set the `gemfile` option below to the name you gave # the file. # (Generate lock file by running `bundle install --gemfile=.overcommit_gems.rb`) +# +# NOTE: the following line will be parsed by a regexp rather than a proper YAML +# parser, so avoid any values other than false or a string, and don't use inline +# comments gemfile: false # Where to store hook plugins specific to a repository. These are loaded in From 11ef06b6eb3398e5891e8d9d2698c4edf2318f1f Mon Sep 17 00:00:00 2001 From: raul Date: Mon, 3 Mar 2025 07:22:25 +0100 Subject: [PATCH 15/16] adds 'set' requirements to diff hook context. (#866) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Running `overcommit` with the `--diff` flag results in errors for version `0.67.0`. ```sh bundle exec overcommit --diff HEAD ``` It results on the following: ```ruby Running pre-commit hooks Check for case-insensitivity conflicts................[CaseConflicts] FAILED Hook raised unexpected error uninitialized constant Overcommit::Hook::PreCommit::CaseConflicts::Set repo_files = Set.new(applicable_files) ^^^ .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook/pre_commit/case_conflicts.rb:8:in `run' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook/base.rb:47:in `block in run_and_transform' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/utils.rb:260:in `with_environment' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook/base.rb:47:in `run_and_transform' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook_runner.rb:162:in `run_hook' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook_runner.rb:98:in `block in consume' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook_runner.rb:94:in `loop' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook_runner.rb:94:in `consume' Analyze with RuboCop........................................[RuboCop] FAILED Hook raised unexpected error uninitialized constant Overcommit::GitRepo::Set lines = Set.new ^^^ .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/git_repo.rb:69:in `extract_modified_lines' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook_context/diff.rb:17:in `modified_lines_in_file' /nix/store/0sj7d3r1kf95f27028j93j0sx3v6p1kw-ruby-3.1.6/lib/ruby/3.1.0/forwardable.rb:238:in `modified_lines_in_file' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/message_processor.rb:145:in `message_on_modified_line?' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/message_processor.rb:138:in `block in remove_ignored_messages' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/message_processor.rb:138:in `select' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/message_processor.rb:138:in `remove_ignored_messages' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/message_processor.rb:45:in `handle_modified_lines' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/message_processor.rb:39:in `hook_result' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook/base.rb:263:in `process_hook_return_value' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook/base.rb:48:in `run_and_transform' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook_runner.rb:162:in `run_hook' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook_runner.rb:98:in `block in consume' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook_runner.rb:94:in `loop' .bundle/ruby/3.1.0/gems/overcommit-0.67.0/lib/overcommit/hook_runner.rb:94:in `consume' ✗ One or more pre-commit hooks failed ``` The problems are fixed once `set` is required in the context that the sub command is run. `run_all.rb` also requires `set` where the module is [defined](https://github.com/sds/overcommit/blob/main/lib/overcommit/hook_context/run_all.rb#L3). --- lib/overcommit/hook_context/diff.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/overcommit/hook_context/diff.rb b/lib/overcommit/hook_context/diff.rb index 0c4b97e0..3e9aa568 100644 --- a/lib/overcommit/hook_context/diff.rb +++ b/lib/overcommit/hook_context/diff.rb @@ -2,6 +2,8 @@ require 'overcommit/git_repo' +require 'set' + module Overcommit::HookContext # Simulates a pre-commit context based on the diff with another git ref. # From 46c303377c495b1455531218f195c61243a61655 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Sun, 2 Mar 2025 22:23:44 -0800 Subject: [PATCH 16/16] Cut version 0.67.1 --- CHANGELOG.md | 4 ++++ lib/overcommit/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b99417a..ad1cb477 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Overcommit Changelog +## 0.67.1 + +* Fix `set` gem dependency error when running with `--diff` flag + ## 0.67.0 * Fix bug introduced in 0.65.0 that prevented `gemfile: false` from working correctly diff --git a/lib/overcommit/version.rb b/lib/overcommit/version.rb index 803dbbfc..872c5327 100644 --- a/lib/overcommit/version.rb +++ b/lib/overcommit/version.rb @@ -2,5 +2,5 @@ # Defines the gem version. module Overcommit - VERSION = '0.67.0' + VERSION = '0.67.1' end