overcommit 0.65.0 → 0.66.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41a718c287aaf78ecc46c67640e13a782af0264bc5054b2c94b027ed50d224a0
4
- data.tar.gz: 5ef6333527352239ff20acc433cd38c7078d8a9fce3f35bb8705091a967adb0f
3
+ metadata.gz: f4144beeaafe295f3b4e5ea046378b5027fb2bf661c1675ccbe8a479caa608de
4
+ data.tar.gz: 640349096a637cc94e04c0848bedda4b9c32c7e7a1bad0b68d2337b42cab3ebc
5
5
  SHA512:
6
- metadata.gz: 9194831e0305eca46d86b60dcaacd1ac1c1e743b13115feec2a94ec8120bb5fbc79338a1b3144480b80ef9cf90e7278da9665b2a6accc54429c07099a1f13db8
7
- data.tar.gz: 7f6163d8a6d612af25422e6c98384657331ab1960c9753d46bbbe302d2e88205d8983f0094593c3f37d4fab0d293cb2a4471710c06e840b046c98baf0e7ce3e0
6
+ metadata.gz: 865f9c330bccfb537d2eedbfd1d67e02b925950273d1965103a1234a3fb54368d6a34f4241d18569f7c4e0fe61153af29f8c8915588b8026e4d0e20702649d01
7
+ data.tar.gz: b45c66fc03f4911105a4a98c15bf5f42e54bf5466ea6f465f47104ce12a94117a6ba410c405a999371449c73688197e308a34dfdf427ae5ae0c6018020f72eac
@@ -9,6 +9,7 @@ module Overcommit
9
9
  class CLI # rubocop:disable Metrics/ClassLength
10
10
  def initialize(arguments, input, logger)
11
11
  @arguments = arguments
12
+ @cli_options = {}
12
13
  @input = input
13
14
  @log = logger
14
15
  @options = {}
@@ -28,6 +29,8 @@ module Overcommit
28
29
  sign
29
30
  when :run_all
30
31
  run_all
32
+ when :diff
33
+ diff
31
34
  end
32
35
  rescue Overcommit::Exceptions::ConfigurationSignatureChanged => e
33
36
  puts e
@@ -45,7 +48,7 @@ module Overcommit
45
48
  @parser = create_option_parser
46
49
 
47
50
  begin
48
- @parser.parse!(@arguments)
51
+ @parser.parse!(@arguments, into: @cli_options)
49
52
 
50
53
  # Default action is to install
51
54
  @options[:action] ||= :install
@@ -98,6 +101,11 @@ module Overcommit
98
101
  @options[:action] = :run_all
99
102
  @options[:hook_to_run] = arg ? arg.to_s : 'run-all'
100
103
  end
104
+
105
+ opts.on('--diff [ref]', 'Run pre_commit hooks against the diff between a given ref. Defaults to `main`.') do |arg| # rubocop:disable Layout/LineLength
106
+ @options[:action] = :diff
107
+ arg
108
+ end
101
109
  end
102
110
 
103
111
  def add_other_options(opts)
@@ -209,6 +217,19 @@ module Overcommit
209
217
  halt(status ? 0 : 65)
210
218
  end
211
219
 
220
+ def diff
221
+ empty_stdin = File.open(File::NULL) # pre-commit hooks don't take input
222
+ context = Overcommit::HookContext.create('diff', config, @arguments, empty_stdin, **@cli_options) # rubocop:disable Layout/LineLength
223
+ config.apply_environment!(context, ENV)
224
+
225
+ printer = Overcommit::Printer.new(config, log, context)
226
+ runner = Overcommit::HookRunner.new(config, log, context, printer)
227
+
228
+ status = runner.run
229
+
230
+ halt(status ? 0 : 65)
231
+ end
232
+
212
233
  # Used for ease of stubbing in tests
213
234
  def halt(status = 0)
214
235
  exit status
@@ -18,10 +18,12 @@ module Overcommit::HookContext
18
18
  # @param config [Overcommit::Configuration]
19
19
  # @param args [Array<String>]
20
20
  # @param input [IO] standard input stream
21
- def initialize(config, args, input)
21
+ # @param options [Hash] cli options
22
+ def initialize(config, args, input, **options)
22
23
  @config = config
23
24
  @args = args
24
25
  @input = input
26
+ @options = options
25
27
  end
26
28
 
27
29
  # Executes a command as if it were a regular git hook, passing all
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'overcommit/git_repo'
4
+
5
+ module Overcommit::HookContext
6
+ # Simulates a pre-commit context based on the diff with another git ref.
7
+ #
8
+ # This results in pre-commit hooks running against the changes between the current
9
+ # and another ref, which is useful for automated CI scripts.
10
+ class Diff < Base
11
+ def modified_files
12
+ @modified_files ||= Overcommit::GitRepo.modified_files(refs: @options[:diff])
13
+ end
14
+
15
+ def modified_lines_in_file(file)
16
+ @modified_lines ||= {}
17
+ @modified_lines[file] ||= Overcommit::GitRepo.extract_modified_lines(file,
18
+ refs: @options[:diff])
19
+ end
20
+
21
+ def hook_class_name
22
+ 'PreCommit'
23
+ end
24
+
25
+ def hook_type_name
26
+ 'pre_commit'
27
+ end
28
+
29
+ def hook_script_name
30
+ 'pre-commit'
31
+ end
32
+
33
+ def initial_commit?
34
+ @initial_commit ||= Overcommit::GitRepo.initial_commit?
35
+ end
36
+ end
37
+ end
@@ -2,13 +2,13 @@
2
2
 
3
3
  # Utility module which manages the creation of {HookContext}s.
4
4
  module Overcommit::HookContext
5
- def self.create(hook_type, config, args, input)
5
+ def self.create(hook_type, config, args, input, **cli_options)
6
6
  hook_type_class = Overcommit::Utils.camel_case(hook_type)
7
7
  underscored_hook_type = Overcommit::Utils.snake_case(hook_type)
8
8
 
9
9
  require "overcommit/hook_context/#{underscored_hook_type}"
10
10
 
11
- Overcommit::HookContext.const_get(hook_type_class).new(config, args, input)
11
+ Overcommit::HookContext.const_get(hook_type_class).new(config, args, input, **cli_options)
12
12
  rescue LoadError, NameError => e
13
13
  # Could happen when a symlink was created for a hook type Overcommit does
14
14
  # not yet support.
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  module Overcommit
5
- VERSION = '0.65.0'
5
+ VERSION = '0.66.0'
6
6
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: overcommit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.65.0
4
+ version: 0.66.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane da Silva
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-27 00:00:00.000000000 Z
10
+ date: 2025-01-30 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: childprocess
@@ -261,6 +261,7 @@ files:
261
261
  - lib/overcommit/hook_context.rb
262
262
  - lib/overcommit/hook_context/base.rb
263
263
  - lib/overcommit/hook_context/commit_msg.rb
264
+ - lib/overcommit/hook_context/diff.rb
264
265
  - lib/overcommit/hook_context/helpers/file_modifications.rb
265
266
  - lib/overcommit/hook_context/helpers/stash_unstaged_changes.rb
266
267
  - lib/overcommit/hook_context/post_checkout.rb