Skip to content

Commit 027e88b

Browse files
committed
Fix amending commits with Unicode command line arguments
1 parent 7624e70 commit 027e88b

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Overcommit Changelog
22

3+
## master (unreleased)
4+
5+
* Fix bug where amending a commit with command line arguments containing
6+
Unicode characters could cause a crash due to invalid byte sequences
7+
38
## 0.32.0.rc1
49

510
* Add `concurrency` global option allowing you to specify the number of threads

lib/overcommit/hook_context/pre_commit.rb

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def amendment?
1515
cmd = Overcommit::Utils.parent_command
1616
amend_pattern = 'commit(\s.*)?\s--amend(\s|$)'
1717

18+
# Since the ps command can return invalid byte sequences for commands
19+
# containing unicode characters, we replace the offending characters,
20+
# since the pattern we're looking for will consist of ASCII characters
21+
unless cmd.valid_encoding?
22+
cmd = cmd.encode('UTF-16be', invalid: :replace, replace: '?').encode('UTF-8')
23+
end
24+
1825
return @amendment if
1926
# True if the command is a commit with the --amend flag
2027
@amendment = !(/\s#{amend_pattern}/ =~ cmd).nil?

spec/overcommit/hook_context/pre_commit_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
it { should == true }
2121
end
2222

23+
context 'when the parent command contains invalid byte sequence' do
24+
let(:command) { "git commit --amend -m \xE3M^AM^B" }
25+
26+
it { should == true }
27+
end
28+
2329
context 'when amending a commit using a git alias' do
2430
around do |example|
2531
repo do

0 commit comments

Comments
 (0)