Skip to content

Commit 696761d

Browse files
committed
Fix a ruby 1.9 encoding problem,
"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.
1 parent a9c027c commit 696761d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lib/grit/git-ruby/internal/loose.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def [](sha1)
3636
end
3737

3838
def get_raw_object(buf)
39-
if buf.length < 2
39+
if buf.bytesize < 2
4040
raise LooseObjectError, "object file too small"
4141
end
4242

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

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

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

8686
# simply figure out the sha
8787
def self.calculate_sha(content, type)
88-
size = content.length.to_s
88+
size = content.bytesize.to_s
8989
verify_header(type, size)
9090
header = "#{type} #{size}\0"
9191
store = header + content
@@ -109,7 +109,7 @@ def unpack_object_header_gently(buf)
109109
size = c & 15;
110110
shift = 4;
111111
while c & 0x80 != 0
112-
if buf.length <= used
112+
if buf.bytesize <= used
113113
raise LooseObjectError, "object file too short"
114114
end
115115
c = buf.getord(used)

0 commit comments

Comments
 (0)