blob: 78de164879238d84f0cf9d558bfb13f1af001cb4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#
# converted from the gitrb project
#
# authors:
# Matthias Lederhofer <matled@gmx.net>
# Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
#
# provides native ruby access to git objects and pack files
#
begin
require 'mmap'
rescue LoadError
module Git
module Raw
module Internal
class Mmap
def initialize(file)
@file = file
@offset = nil
end
def unmap
@file = nil
end
def [](*idx)
idx = idx[0] if idx.length == 1
case idx
when Range
offset = idx.first
len = idx.last - idx.first + idx.exclude_end? ? 0 : 1
when Fixnum
offset = idx
len = nil
when Array
offset, len = idx
else
raise RuntimeError, "invalid index param: #{idx.class}"
end
if @offset != offset
@file.seek(offset)
end
@offset = offset + len ? len : 1
if not len
@file.read(1)[0]
else
@file.read(len)
end
end
end
end
end
end
end # rescue LoadError
|