Skip to content

Commit 90dea6d

Browse files
author
scott Chacon
committed
have the pure ruby bindings working to some degree
1 parent d07a2c0 commit 90dea6d

File tree

8 files changed

+373
-393
lines changed

8 files changed

+373
-393
lines changed

lib/git.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
require 'git/status'
2424
require 'git/author'
2525

26+
require 'git/raw/repository'
27+
28+
2629
# Git/Ruby Library
2730
#
2831
# This provides bindings for working with git in complex

lib/git/raw/git.rb

Lines changed: 0 additions & 63 deletions
This file was deleted.

lib/git/raw/internal/loose.rb

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,89 @@
33

44
require 'git/raw/internal/object'
55

6-
module Git module Raw module Internal
7-
class LooseObjectError < StandardError
8-
end
6+
module Git
7+
module Raw
8+
module Internal
9+
class LooseObjectError < StandardError
10+
end
911

10-
class LooseStorage
11-
def initialize(directory)
12-
@directory = directory
13-
end
12+
class LooseStorage
13+
def initialize(directory)
14+
@directory = directory
15+
end
1416

15-
def [](sha1)
16-
sha1 = sha1.unpack("H*")[0]
17+
def [](sha1)
18+
sha1 = sha1.unpack("H*")[0]
1719

18-
path = @directory+'/'+sha1[0...2]+'/'+sha1[2..40]
19-
begin
20-
get_raw_object(File.read(path))
21-
rescue Errno::ENOENT
22-
nil
23-
end
24-
end
20+
path = @directory+'/'+sha1[0...2]+'/'+sha1[2..40]
21+
begin
22+
get_raw_object(File.read(path))
23+
rescue Errno::ENOENT
24+
nil
25+
end
26+
end
2527

26-
def get_raw_object(buf)
27-
if buf.length < 2
28-
raise LooseObjectError, "object file too small"
29-
end
28+
def get_raw_object(buf)
29+
if buf.length < 2
30+
raise LooseObjectError, "object file too small"
31+
end
3032

31-
if legacy_loose_object?(buf)
32-
content = Zlib::Inflate.inflate(buf)
33-
header, content = content.split(/\0/, 2)
34-
if !header || !content
35-
raise LooseObjectError, "invalid object header"
36-
end
37-
type, size = header.split(/ /, 2)
38-
if !%w(blob tree commit tag).include?(type) || size !~ /^\d+$/
39-
raise LooseObjectError, "invalid object header"
33+
if legacy_loose_object?(buf)
34+
content = Zlib::Inflate.inflate(buf)
35+
header, content = content.split(/\0/, 2)
36+
if !header || !content
37+
raise LooseObjectError, "invalid object header"
38+
end
39+
type, size = header.split(/ /, 2)
40+
if !%w(blob tree commit tag).include?(type) || size !~ /^\d+$/
41+
raise LooseObjectError, "invalid object header"
42+
end
43+
type = type.to_sym
44+
size = size.to_i
45+
else
46+
type, size, used = unpack_object_header_gently(buf)
47+
content = Zlib::Inflate.inflate(buf[used..-1])
48+
end
49+
raise LooseObjectError, "size mismatch" if content.length != size
50+
return RawObject.new(type, content)
4051
end
41-
type = type.to_sym
42-
size = size.to_i
43-
else
44-
type, size, used = unpack_object_header_gently(buf)
45-
content = Zlib::Inflate.inflate(buf[used..-1])
46-
end
47-
raise LooseObjectError, "size mismatch" if content.length != size
48-
return RawObject.new(type, content)
49-
end
5052

51-
# private
52-
def unpack_object_header_gently(buf)
53-
used = 0
54-
c = buf[used]
55-
used += 1
53+
# private
54+
def unpack_object_header_gently(buf)
55+
used = 0
56+
c = buf[used]
57+
used += 1
58+
59+
type = (c >> 4) & 7;
60+
size = c & 15;
61+
shift = 4;
62+
while c & 0x80 != 0
63+
if buf.length <= used
64+
raise LooseObjectError, "object file too short"
65+
end
66+
c = buf[used]
67+
used += 1
5668

57-
type = (c >> 4) & 7;
58-
size = c & 15;
59-
shift = 4;
60-
while c & 0x80 != 0
61-
if buf.length <= used
62-
raise LooseObjectError, "object file too short"
69+
size += (c & 0x7f) << shift
70+
shift += 7
71+
end
72+
type = OBJ_TYPES[type]
73+
if ![:blob, :tree, :commit, :tag].include?(type)
74+
raise LooseObjectError, "invalid loose object type"
75+
end
76+
return [type, size, used]
6377
end
64-
c = buf[used]
65-
used += 1
78+
private :unpack_object_header_gently
6679

67-
size += (c & 0x7f) << shift
68-
shift += 7
69-
end
70-
type = OBJ_TYPES[type]
71-
if ![:blob, :tree, :commit, :tag].include?(type)
72-
raise LooseObjectError, "invalid loose object type"
80+
def legacy_loose_object?(buf)
81+
word = (buf[0] << 8) + buf[1]
82+
buf[0] == 0x78 && word % 31 == 0
83+
end
84+
private :legacy_loose_object?
7385
end
74-
return [type, size, used]
75-
end
76-
private :unpack_object_header_gently
77-
78-
def legacy_loose_object?(buf)
79-
word = (buf[0] << 8) + buf[1]
80-
buf[0] == 0x78 && word % 31 == 0
81-
end
82-
private :legacy_loose_object?
86+
end
8387
end
84-
end end
88+
end
8589

8690
if $0 == __FILE__
8791
require 'find'

lib/git/raw/internal/mmap.rb

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,47 @@
22
require 'mmap'
33
rescue LoadError
44

5-
module Git module Raw module Internal
6-
class Mmap
7-
def initialize(file)
8-
@file = file
9-
@offset = nil
10-
end
5+
module Git
6+
module Raw
7+
module Internal
8+
class Mmap
9+
def initialize(file)
10+
@file = file
11+
@offset = nil
12+
end
1113

12-
def unmap
13-
@file = nil
14-
end
14+
def unmap
15+
@file = nil
16+
end
1517

16-
def [](*idx)
17-
idx = idx[0] if idx.length == 1
18-
case idx
19-
when Range
20-
offset = idx.first
21-
len = idx.last - idx.first + idx.exclude_end? ? 0 : 1
22-
when Fixnum
23-
offset = idx
24-
len = nil
25-
when Array
26-
offset, len = idx
27-
else
28-
raise RuntimeError, "invalid index param: #{idx.class}"
29-
end
30-
if @offset != offset
31-
@file.seek(offset)
32-
end
33-
@offset = offset + len ? len : 1
34-
if not len
35-
@file.read(1)[0]
36-
else
37-
@file.read(len)
18+
def [](*idx)
19+
idx = idx[0] if idx.length == 1
20+
case idx
21+
when Range
22+
offset = idx.first
23+
len = idx.last - idx.first + idx.exclude_end? ? 0 : 1
24+
when Fixnum
25+
offset = idx
26+
len = nil
27+
when Array
28+
offset, len = idx
29+
else
30+
raise RuntimeError, "invalid index param: #{idx.class}"
31+
end
32+
if @offset != offset
33+
@file.seek(offset)
34+
end
35+
@offset = offset + len ? len : 1
36+
if not len
37+
@file.read(1)[0]
38+
else
39+
@file.read(len)
40+
end
41+
end
3842
end
3943
end
40-
end
41-
end end
44+
end
45+
end
4246

4347
end # rescue LoadError
4448

lib/git/raw/internal/object.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
require 'digest/sha1'
22

3-
module Git module Raw module Internal
4-
OBJ_NONE = 0
5-
OBJ_COMMIT = 1
6-
OBJ_TREE = 2
7-
OBJ_BLOB = 3
8-
OBJ_TAG = 4
3+
module Git
4+
module Raw
5+
module Internal
6+
OBJ_NONE = 0
7+
OBJ_COMMIT = 1
8+
OBJ_TREE = 2
9+
OBJ_BLOB = 3
10+
OBJ_TAG = 4
911

10-
OBJ_TYPES = [nil, :commit, :tree, :blob, :tag].freeze
12+
OBJ_TYPES = [nil, :commit, :tree, :blob, :tag].freeze
1113

12-
class RawObject
13-
attr_accessor :type, :content
14-
def initialize(type, content)
15-
@type = type
16-
@content = content
17-
end
14+
class RawObject
15+
attr_accessor :type, :content
16+
def initialize(type, content)
17+
@type = type
18+
@content = content
19+
end
1820

19-
def sha1
20-
Digest::SHA1.digest("%s %d\0" % [@type, @content.length] + @content)
21-
end
21+
def sha1
22+
Digest::SHA1.digest("%s %d\0" % [@type, @content.length] + @content)
23+
end
24+
end
25+
end
2226
end
23-
end end
27+
end

0 commit comments

Comments
 (0)