Skip to content

Assume strings are utf8 by default and fallback on encoding if necessary #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: ruby
rvm:
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.1
Expand Down
2 changes: 1 addition & 1 deletion git.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.version = '1.3.0'

s.require_paths = ['lib']
s.required_ruby_version = '>= 1.9'
s.required_ruby_version = '>= 1.9.3'
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.requirements = ['git 1.6.0.0, or greater']

Expand Down
25 changes: 20 additions & 5 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -859,13 +859,28 @@ def meets_required_version?

def command_lines(cmd, opts = [], chdir = true, redirect = '')
cmd_op = command(cmd, opts, chdir)
op = cmd_op.encode("UTF-8", "binary", {
:invalid => :replace,
:undef => :replace
})
op.split("\n")
split_utf8(cmd_op)
end

def split_utf8(s)
# ruby can think the string is utf8 but any action on the strings chars
# will trigger errors. Here we try to split utf8 strings.
# It it isn't utf8 or if it fails, we fallback to assume the input is
# "binary" encoding
result = begin
s.encoding == Encoding::UTF_8 && s.split("\n")
rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError
nil
end

result ||= s.encode("UTF-8", "binary", {
:invalid => :replace,
:undef => :replace,
}).split("\n")
result
end


# Takes the current git's system ENV variables and store them.
def store_git_system_env_variables
@git_system_env_variables = {}
Expand Down