Skip to content

Commit 607ce05

Browse files
authored
Get suite running smoothly again (#824)
Am planning on proposing/implementing a new feature, but first I noticed that there are a number of failing specs on main, so I figured I would start with that. As written, this includes four main adjustments, two of which might actually be bugfixes: - one spec was hanging indefinitely for me locally on a `Thread.join` call - explicitly capturing the child threads and joining them directly fixed it (might be machine dependent?) - when an encoding is specified, the rails schema up to date hook was passing arguments incorrectly (kwarg containing `encoding` was being interpreted as the `length` positional arg) (possible bug) - many specs were attempting to create file submodules without the `protocol.file.allow=always` config option causing failure - alias detection for amendment appeared to be broken, due to regexp incompatibilities between ruby/git (git wasn't properly parsing `\s` in the value matcher for `git config --regexp [key] [value]` command), so moved that matching into ruby (possible bug, also maybe machine/git build opt dependent) - dropped ruby 2.6 from the test matrix - it appears that it's far enough EOL that github has trouble even setting it up. I can attempt to add it back/get it working, but I figured it might be time to ditch it *I don't have a windows machine, so I'm not gonna be much help with those CI builds (should note fails appear unrelated to anything I've done here).
1 parent f190f24 commit 607ce05

File tree

13 files changed

+54
-42
lines changed

13 files changed

+54
-42
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
fail-fast: false
1515
matrix:
1616
ruby-version:
17-
- '2.6'
1817
- '2.7'
1918
- '3.0'
2019
- '3.1'

lib/overcommit/hook/pre_commit/rails_schema_up_to_date.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def run # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplex
3535
private
3636

3737
def encoding
38-
return unless @config.key?('encoding')
38+
return {} unless @config.key?('encoding')
3939

4040
{ encoding: @config['encoding'] }.compact
4141
end
@@ -53,7 +53,7 @@ def schema_files
5353
end
5454

5555
def schema
56-
@schema ||= schema_files.map { |file| File.read(file, encoding) }.join
56+
@schema ||= schema_files.map { |file| File.read(file, **encoding) }.join
5757
@schema.tr('_', '')
5858
end
5959

lib/overcommit/hook_context/helpers/file_modifications.rb

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def amendment?
1212
cmd = Overcommit::Utils.parent_command
1313
return unless cmd
1414

15-
amend_pattern = 'commit(\s.*)?\s--amend(\s|$)'
15+
amend_pattern = /commit(\s.*)?\s--amend/
1616

1717
# Since the ps command can return invalid byte sequences for commands
1818
# containing unicode characters, we replace the offending characters,
@@ -24,18 +24,11 @@ def amendment?
2424
encode('UTF-8')
2525
end
2626

27-
return @amendment if
28-
# True if the command is a commit with the --amend flag
29-
@amendment = !(/\s#{amend_pattern}/ =~ cmd).nil?
27+
# True if the command is a commit with the --amend flag
28+
return @amendment if @amendment = cmd.match?(amend_pattern)
3029

3130
# Check for git aliases that call `commit --amend`
32-
`git config --get-regexp "^alias\\." "#{amend_pattern}"`.
33-
scan(/alias\.([-\w]+)/). # Extract the alias
34-
each do |match|
35-
return @amendment if
36-
# True if the command uses a git alias for `commit --amend`
37-
@amendment = !(/git(\.exe)?\s+#{match[0]}/ =~ cmd).nil?
38-
end
31+
return @amendment if @amendment = command_is_amend_alias?(cmd, amend_pattern)
3932

4033
@amendment
4134
end
@@ -74,6 +67,22 @@ def modified_lines_in_file(file)
7467
end
7568
@modified_lines[file]
7669
end
70+
71+
private
72+
73+
def command_is_amend_alias?(cmd, amend_pattern)
74+
`git config --get-regexp "^alias"`.split("\n").each do |alias_def|
75+
alias_map = alias_def.match /alias\.(?<to>[-\w]+)\s+(?<from>.+)/
76+
next unless alias_map
77+
78+
alias_from_match = alias_map[:from].match? amend_pattern
79+
alias_to_match = cmd.match? /git(\.exe)?\s+#{alias_map[:to]}/
80+
81+
# True if the command uses a git alias for `commit --amend`
82+
return true if @amendment = alias_from_match && alias_to_match
83+
end
84+
false
85+
end
7786
end
7887
end
7988
end

spec/overcommit/git_repo_spec.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
end
2525

2626
submodule = repo do
27-
`git submodule add #{nested_submodule} nested-sub 2>&1 > #{File::NULL}`
27+
git_config = '-c protocol.file.allow=always'
28+
`git #{git_config} submodule add #{nested_submodule} nested-sub 2>&1 > #{File::NULL}`
2829
`git commit -m "Add nested submodule"`
2930
end
3031

3132
repo do
32-
`git submodule add #{submodule} sub 2>&1 > #{File::NULL}`
33+
`git -c protocol.file.allow=always submodule add #{submodule} sub 2>&1 > #{File::NULL}`
3334
example.run
3435
end
3536
end
@@ -150,7 +151,7 @@
150151
end
151152

152153
before do
153-
`git submodule add #{submodule} sub 2>&1 > #{File::NULL}`
154+
`git -c protocol.file.allow=always submodule add #{submodule} sub 2>&1 > #{File::NULL}`
154155
end
155156

156157
it { should_not include File.expand_path('sub') }
@@ -178,7 +179,8 @@
178179
`git commit --allow-empty -m "Submodule commit"`
179180
end
180181

181-
`git submodule add #{submodule} #{submodule_dir} 2>&1 > #{File::NULL}`
182+
git_config = '-c protocol.file.allow=always'
183+
`git #{git_config} submodule add #{submodule} #{submodule_dir} 2>&1 > #{File::NULL}`
182184
`git commit -m "Add submodule"`
183185
end
184186

@@ -282,7 +284,7 @@
282284
touch 'tracked'
283285
`git add tracked`
284286
`git commit -m "Initial commit"`
285-
`git submodule add #{submodule} sub 2>&1 > #{File::NULL}`
287+
`git -c protocol.file.allow=always submodule add #{submodule} sub 2>&1 > #{File::NULL}`
286288
touch 'staged'
287289
`git add staged`
288290
example.run
@@ -327,7 +329,7 @@
327329
end
328330

329331
repo do
330-
`git submodule add #{submodule} sub-repo 2>&1 > #{File::NULL}`
332+
`git -c protocol.file.allow=always submodule add #{submodule} sub-repo 2>&1 > #{File::NULL}`
331333
`git commit -m "Initial commit"`
332334
example.run
333335
end
@@ -343,7 +345,8 @@
343345
`git commit --allow-empty -m "Another submodule"`
344346
end
345347

346-
`git submodule add #{another_submodule} another-sub-repo 2>&1 > #{File::NULL}`
348+
git_config = '-c protocol.file.allow=always'
349+
`git #{git_config} submodule add #{another_submodule} another-sub-repo 2>&1 > #{File::NULL}`
347350
end
348351

349352
it { should be_empty }
@@ -365,11 +368,12 @@
365368

366369
context 'when there are multiple submodule removals staged' do
367370
before do
368-
another_submodule = repo do
371+
another_submod = repo do
369372
`git commit --allow-empty -m "Another submodule"`
370373
end
371374

372-
`git submodule add #{another_submodule} yet-another-sub-repo 2>&1 > #{File::NULL}`
375+
git_conf = '-c protocol.file.allow=always'
376+
`git #{git_conf} submodule add #{another_submod} yet-another-sub-repo 2>&1 > #{File::NULL}`
373377
`git commit -m "Add yet another submodule"`
374378
`git rm sub-repo`
375379
`git rm yet-another-sub-repo`

spec/overcommit/hook/prepare_commit_msg/base_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
contents + "bravo\n"
3939
end
4040
end
41-
Thread.new { hook_1.run }
42-
Thread.new { hook_2.run }
43-
Thread.list.each { |t| t.join unless t == Thread.current }
41+
[Thread.new { hook_1.run }, Thread.new { hook_2.run }].each(&:join)
4442
expect(File.read(tempfile)).to match(/alpha\n#{initial_content}bravo\n/m)
4543
end
4644
end

spec/overcommit/hook_context/commit_msg_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@
298298
end
299299

300300
repo do
301-
`git submodule add #{submodule} sub > #{File::NULL} 2>&1`
301+
`git -c protocol.file.allow=always submodule add #{submodule} sub > #{File::NULL} 2>&1`
302302
`git commit -m "Add submodule"`
303303
echo('Hello World', 'sub/submodule-file')
304304
`git submodule foreach "git add submodule-file" < #{File::NULL}`
@@ -474,7 +474,7 @@
474474
end
475475

476476
repo do
477-
`git submodule add #{submodule} sub > #{File::NULL} 2>&1`
477+
`git -c protocol.file.allow=always submodule add #{submodule} sub > #{File::NULL} 2>&1`
478478
`git commit -m "Add submodule"`
479479
echo('Hello World', 'sub/submodule-file')
480480
`git submodule foreach "git add submodule-file" < #{File::NULL}`
@@ -500,7 +500,7 @@
500500
end
501501

502502
repo do
503-
`git submodule add #{submodule} sub > #{File::NULL} 2>&1`
503+
`git -c protocol.file.allow=always submodule add #{submodule} sub > #{File::NULL} 2>&1`
504504
`git commit -m "Add submodule"`
505505
echo('Hello World', 'sub/submodule-file')
506506
`git submodule foreach "git add submodule-file" < #{File::NULL}`
@@ -532,7 +532,7 @@
532532
end
533533

534534
repo do
535-
`git submodule add #{submodule} sub > #{File::NULL} 2>&1`
535+
`git -c protocol.file.allow=always submodule add #{submodule} sub > #{File::NULL} 2>&1`
536536
`git commit -m "Add submodule"`
537537
`git rm sub`
538538
example.run
@@ -561,7 +561,7 @@
561561
end
562562

563563
repo do
564-
`git submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
564+
`git -c protocol.file.allow=always submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
565565
expect(subject).to_not include File.expand_path('test-sub')
566566
end
567567
end

spec/overcommit/hook_context/post_checkout_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
repo do
6969
`git commit --allow-empty -m "Initial commit"`
70-
`git submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
70+
`git -c protocol.file.allow=always submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
7171
`git commit -m "Add submodule"`
7272
expect(subject).to_not include File.expand_path('test-sub')
7373
end

spec/overcommit/hook_context/post_commit_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
end
2121

2222
repo do
23-
`git submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
23+
`git -c protocol.file.allow=always submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
2424
`git commit -m "Initial commit"`
2525
expect(subject).to_not include File.expand_path('test-sub')
2626
end

spec/overcommit/hook_context/post_merge_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
repo do
9595
`git commit --allow-empty -m "Initial commit"`
9696
`git checkout -b child > #{File::NULL} 2>&1`
97-
`git submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
97+
`git -c protocol.file.allow=always submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
9898
`git commit -m "Add submodule"`
9999
`git checkout master > #{File::NULL} 2>&1`
100100
`git merge --no-ff --no-edit child`

spec/overcommit/hook_context/post_rewrite_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@
101101
end
102102

103103
repo do
104+
git_config = '-c protocol.file.allow=always'
104105
`git commit --allow-empty -m "Initial commit"`
105-
`git submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
106+
`git #{git_config} submodule add #{submodule} test-sub 2>&1 > #{File::NULL}`
106107
`git commit --amend -m "Add submodule"`
107108
expect(subject).to_not include File.expand_path('test-sub')
108109
end

0 commit comments

Comments
 (0)