Skip to content

Commit

Permalink
Fix a ruby 1.9 encoding problem,
Browse files Browse the repository at this point in the history
"LooseObjectError: size mismatch" by committing non-ASCII content.

String#length returns number of characters in the encoding of the string. Instead, we should use String#bytesize.
  • Loading branch information
hiroshi committed Jan 2, 2011
1 parent a9c027c commit 696761d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/grit/git-ruby/internal/loose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def [](sha1)
end

def get_raw_object(buf)
if buf.length < 2
if buf.bytesize < 2
raise LooseObjectError, "object file too small"
end

Expand All @@ -56,14 +56,14 @@ def get_raw_object(buf)
type, size, used = unpack_object_header_gently(buf)
content = Zlib::Inflate.inflate(buf[used..-1])
end
raise LooseObjectError, "size mismatch" if content.length != size
raise LooseObjectError, "size mismatch" if content.bytesize != size
return RawObject.new(type, content)
end

# currently, I'm using the legacy format because it's easier to do
# this function takes content and a type and writes out the loose object and returns a sha
def put_raw_object(content, type)
size = content.length.to_s
size = content.bytesize.to_s
LooseStorage.verify_header(type, size)

header = "#{type} #{size}\0"
Expand All @@ -85,7 +85,7 @@ def put_raw_object(content, type)

# simply figure out the sha
def self.calculate_sha(content, type)
size = content.length.to_s
size = content.bytesize.to_s
verify_header(type, size)
header = "#{type} #{size}\0"
store = header + content
Expand All @@ -109,7 +109,7 @@ def unpack_object_header_gently(buf)
size = c & 15;
shift = 4;
while c & 0x80 != 0
if buf.length <= used
if buf.bytesize <= used
raise LooseObjectError, "object file too short"
end
c = buf.getord(used)
Expand Down

2 comments on commit 696761d

@pauldowman
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hiroshi Thank you! I just spent a day tracking down a bug that turned out to be this. :-P

@technoweenie Any idea when there will be a new release of Grit with this commit in it? Looks like it just missed 2.4.1.

@pal
Copy link

@pal pal commented on 696761d Oct 3, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also hoping for a new official release to fix this error, any updates?

Please sign in to comment.