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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#!/usr/bin/env lua
local build_dir = arg[1]
local git_path = arg[2] or "./tests/test_rep/.git/"
-- Make it easier to test
if ( build_dir ) then
package.cpath = build_dir .. "?.so;" .. package.cpath
end
local git2 = require"git2"
require"utils"
print(dump(git2))
local function dump_obj(obj)
print('dump OdbObject:', obj)
if obj == nil then
return
end
print('id = ', obj:id())
print('type = ', obj:type())
end
-- create odb
local db = assert(git2.ODB.new())
print("=============================================== new db=", db)
print("dump ODB interface")
print(dbg_dump(db))
-- create backend
local obj_cache = {}
local function get_obj(oid)
print("------------------- exists callback:", oid)
if not oid then
return nil
end
-- convert oid to string.
oid = tostring(oid)
return obj_cache[oid]
end
local cbs = {
on_read = function(oid)
print("------------------- read callback:", oid)
local obj = get_obj(oid)
if not obj then
return nil, git2.ENOTFOUND
end
return obj.data, obj.otype
end,
on_read_prefix = function(short_oid, len)
print("------------------- read_prefix callback:", oid)
local obj = get_obj(short_oid)
if not obj then
if len ~= git2.OID.HEXSZ then
return nil, git2.ENOTIMPLEMENTED
end
return nil, git2.ENOTFOUND
end
return obj.data, obj.otype, short_oid
end,
on_read_header = function(oid)
print("------------------- read_header callback:", oid)
local obj = get_obj(oid)
if not obj then
return nil, git2.ENOTFOUND
end
return obj.len, obj.otype
end,
on_write = function(data, otype)
local oid = git2.ODB.hash(data, git2.Object.string2type(otype))
print("------------------- write callback:", data, otype)
if not oid then
return nil, -1
end
-- convert oid to string.
local oid_str = tostring(oid)
-- put raw object in cache
obj_cache[oid_str] = { data = data, len = #data, otype = otype}
return oid
end,
on_exists = function(oid)
print("------------------- exists callback:", oid)
local obj = get_obj(oid)
if not obj then
return false
end
return true
end,
on_free = function()
print("------------------- free callback:")
end,
}
local backend = git2.ODBBackend(cbs)
print('add backend:', assert(db:add_backend(backend, 0)))
print("test writing test blob to odb:")
local oid, err = db:write("any ol content will do", 'blob')
print('write:', oid, err)
print()
print("test reading RawObjects from odb:")
local object_ids = {
{'tree', "31f3d5703ce27f0b63c3eb0d829abdc95b51deae"},
{'commit', "d5a93c463d4cca0068750eb6af7b4b54eea8599b"},
{'blob', "f534deb63f967cddd4bd440d05d3f6f075e55fca"},
{'blob', "275a4019807c7bb7bc80c0ca8903bf84345e1bdf"},
}
for _,obj in ipairs(object_ids) do
local oid = git2.OID.hex(obj[2])
local obj, err = db:read(oid)
print('read', obj, err)
dump_obj(obj)
print()
end
print("Creating repository from git repository:", git_path)
local status, rep = pcall(git2.Repository.open_no_backend,
git_path, git_path .. 'objects', git_path .. 'index', git_path .. '../')
if not status then
rep = assert(git2.Repository.open(git_path))
else
print("Created repository with no backends from git repository:", git_path)
end
db = rep:odb()
print("=============================================== repo db=", db)
backend = git2.ODBBackend(cbs)
print("add backend repository's odb:", assert(db:add_backend(backend, 0)))
print()
print("try reading objects from repository:")
local object_ids = {
{'tree', "31f3d5703ce27f0b63c3eb0d829abdc95b51deae"},
{'commit', "d5a93c463d4cca0068750eb6af7b4b54eea8599b"},
{'blob', "f534deb63f967cddd4bd440d05d3f6f075e55fca"},
{'blob', "275a4019807c7bb7bc80c0ca8903bf84345e1bdf"},
}
for _,obj in ipairs(object_ids) do
local oid = git2.OID.hex(obj[2])
local obj, err = rep:lookup(oid, obj[1])
print('read', obj, err)
print()
end
db = nil
backend = nil
obj_cache = nil
collectgarbage"collect"
collectgarbage"collect"
collectgarbage"collect"
print()
print()
print("finished")
|