Skip to content

Commit 5fc63b9

Browse files
authored
Fix encoding of process output under windows (sds#766)
* Fix encoding of process output under windows Output of commands under windows is not UTF-8 by default, this can lead to "invalid byte sequence in UTF-8" error on sylink check * Fix coding style * Fix invalid converter setting (UTF-8 to UTF-8)
1 parent 8622f9e commit 5fc63b9

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/overcommit/subprocess.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def spawn(args, options = {})
5151
err.rewind
5252
out.rewind
5353

54-
Result.new(process.exit_code, out.read, err.read)
54+
Result.new(process.exit_code, to_utf8(out.read), to_utf8(err.read))
5555
end
5656

5757
# Spawns a new process in the background using the given array of
@@ -83,6 +83,23 @@ def win32_prepare_args(args)
8383
%w[cmd.exe /c] + [args.join(' ')]
8484
end
8585

86+
# Convert string from current locale to utf-8
87+
#
88+
# When running commands under windows the command output is using
89+
# current system locale (depends on system lanuage) not UTF-8
90+
#
91+
# @param process [String]
92+
# @return [String]
93+
def to_utf8(string)
94+
if Encoding.locale_charmap == 'UTF-8'
95+
return string
96+
end
97+
98+
ec = Encoding::Converter.new(Encoding.locale_charmap, 'UTF-8')
99+
# Convert encoding, alternatively simple: string.scrub will suffice
100+
ec.convert(string)
101+
end
102+
86103
# @param process [ChildProcess]
87104
# @return [Array<IO>]
88105
def assign_output_streams(process)

0 commit comments

Comments
 (0)