Skip to content
This repository has been archived by the owner on Apr 19, 2018. It is now read-only.

Handle encoding header of Git commits when possible. #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 0 deletions lib/grit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require 'grit/ruby1.9'

# internal requires
require 'grit/encode'
require 'grit/lazy'
require 'grit/errors'
require 'grit/git-ruby'
Expand Down
8 changes: 8 additions & 0 deletions lib/grit/commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Grit

class Commit
extend Lazy
extend Encode

attr_reader :id
attr_reader :repo
Expand All @@ -25,12 +26,17 @@ class Commit
def self.parse_batch(repo, sha, size, object)
info, message = object.split("\n\n", 2)

encoding = nil

lines = info.split("\n")
tree = lines.shift.split(' ', 2).last
parents = []
parents << lines.shift[7..-1] while lines.first[0, 6] == 'parent'
author, authored_date = Grit::Commit.actor(lines.shift)
committer, committed_date = Grit::Commit.actor(lines.shift)
encoding = $1 if lines.detect { |line| line =~ /\Aencoding\s*(.+)\z/ }

message = message_in_utf8(message, encoding)

Grit::Commit.new(
repo, sha, parents, tree,
Expand Down Expand Up @@ -176,6 +182,8 @@ def self.list_from_string(repo, text)

lines.shift while lines.first && lines.first.empty?

message_lines = message_lines.map { |line| message_in_utf8(line, encoding) } if encoding

commits << Commit.new(repo, id, parents, tree, author, authored_date, committer, committed_date, message_lines)
end

Expand Down
35 changes: 35 additions & 0 deletions lib/grit/encode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Grit
module Encode
ENCODING_MAP = {
'koi8r' => 'KOI8-R',
}

def find_ruby_encoding(encoding)
return nil unless defined?(Encoding)
Encoding.find(ENCODING_MAP[encoding] || encoding)
rescue ArgumentError
warn "Cannot map git encoding to ruby one: #{encoding}"
nil
end

def message_in_utf8(message, encoding)
message = message.dup
message.force_encoding('UTF-8') if message.respond_to?(:force_encoding)
if encoding && encoding !~ /\Autf\-8i\z/i && message && message.respond_to?(:encode!)
ruby_encoding = find_ruby_encoding(encoding)
if ruby_encoding
message.encode!('UTF-8', ruby_encoding, :invalid => :replace, :undef => :replace)
unless message.valid_encoding?
message.encode!('UTF-8', 'ISO-8859-1', :invalid => :replace, :undef => :replace)
end
else
unless message.valid_encoding?
message.encode!('UTF-8', 'ISO-8859-1', :invalid => :replace, :undef => :replace)
end
end
end
message
end
end
end

20 changes: 15 additions & 5 deletions lib/grit/git-ruby/git_object.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# --- encoding: utf-8 ---

# converted from the gitrb project
#
# authors:
Expand Down Expand Up @@ -232,11 +233,13 @@ def actual_raw
end

class Commit < GitObject
attr_accessor :author, :committer, :tree, :parent, :message, :headers
extend Grit::Encode

attr_accessor :author, :committer, :tree, :parent, :message, :encoding, :headers

def self.from_raw(rawobject, repository=nil)
parent = []
tree = author = committer = nil
tree = author = committer = encoding = nil

headers, message = rawobject.content.split(/\n\n/, 2)
all_headers = headers.split(/\n/).map { |header| header.split(/ /, 2) }
Expand All @@ -250,6 +253,8 @@ def self.from_raw(rawobject, repository=nil)
author = UserInfo.new(value)
when "committer"
committer = UserInfo.new(value)
when "encoding"
encoding = value
else
warn "unknown header '%s' in commit %s" % \
[key, rawobject.sha1.unpack("H*")[0]]
Expand All @@ -258,17 +263,22 @@ def self.from_raw(rawobject, repository=nil)
if not tree && author && committer
raise RuntimeError, "incomplete raw commit object"
end
new(tree, parent, author, committer, message, headers, repository)
new(tree, parent, author, committer, message, headers, repository, encoding)
end

def initialize(tree, parent, author, committer, message, headers, repository=nil)
def initialize(tree, parent, author, committer, message, headers, repository=nil, encoding=nil)
@tree = tree
@author = author
@parent = parent
@committer = committer
@message = message
@headers = headers
@repository = repository
@encoding = encoding
end

def decoded_message
message_in_utf8(message, encoding)
end

def type
Expand Down
2 changes: 1 addition & 1 deletion test/test_git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def teardown
end

def test_method_missing
assert_match(/^git version [\w\.]*$/, @git.version)
assert_match(/\Agit version [\w\.\(\)\s\-]*\z/, @git.version)
end

def test_logs_stderr
Expand Down