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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
package filesystem
import (
"fmt"
"io"
"gopkg.in/src-d/go-git.v4/core"
"gopkg.in/src-d/go-git.v4/formats/objfile"
"gopkg.in/src-d/go-git.v4/formats/packfile"
"gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit"
"gopkg.in/src-d/go-git.v4/storage/filesystem/internal/index"
"gopkg.in/src-d/go-git.v4/utils/fs"
)
// ObjectStorage is an implementation of core.ObjectStorage that stores
// data on disk in the standard git format (this is, the .git directory).
//
// Zero values of this type are not safe to use, see the New function below.
//
// Currently only reads are supported, no writting.
//
// Also values from this type are not yet able to track changes on disk, this is,
// Gitdir values will get outdated as soon as repositories change on disk.
type ObjectStorage struct {
dir *dotgit.DotGit
index index.Index
}
func (s *ObjectStorage) NewObject() core.Object {
return &core.MemoryObject{}
}
// Writer method not supported on Memory storage
func (o *ObjectStorage) Writer() (io.WriteCloser, error) {
return o.dir.NewObjectPack()
}
// Set adds a new object to the storage. As this functionality is not
// yet supported, this method always returns a "not implemented yet"
// error an zero hash.
func (s *ObjectStorage) Set(core.Object) (core.Hash, error) {
return core.ZeroHash, fmt.Errorf("not implemented yet")
}
// Get returns the object with the given hash, by searching for it in
// the packfile and the git object directories.
func (s *ObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error) {
obj, err := s.getFromUnpacked(t, h)
if err == dotgit.ErrObjfileNotFound {
if s.index == nil {
return nil, core.ErrObjectNotFound
}
return s.getFromPackfile(t, h)
}
return obj, err
}
func (s *ObjectStorage) getFromUnpacked(t core.ObjectType, h core.Hash) (obj core.Object, err error) {
f, err := s.dir.Object(h)
if err != nil {
return nil, err
}
defer func() {
errClose := f.Close()
if err == nil {
err = errClose
}
}()
obj = s.NewObject()
objReader, err := objfile.NewReader(f)
if err != nil {
return nil, err
}
defer func() {
errClose := objReader.Close()
if err == nil {
err = errClose
}
}()
err = objReader.FillObject(obj)
if err != nil {
return nil, err
}
if core.AnyObject != t && obj.Type() != t {
return nil, core.ErrObjectNotFound
}
return obj, nil
}
// Get returns the object with the given hash, by searching for it in
// the packfile.
func (s *ObjectStorage) getFromPackfile(t core.ObjectType, h core.Hash) (core.Object, error) {
offset, err := s.index.Get(h)
if err != nil {
return nil, err
}
packs, err := s.dir.ObjectsPacks()
if err != nil {
return nil, err
}
if len(packs) == 0 {
return nil, nil
}
f, _, err := s.dir.ObjectPack(packs[0].Name())
defer func() {
errClose := f.Close()
if err == nil {
err = errClose
}
}()
p := packfile.NewScanner(f)
d := packfile.NewDecoder(p, nil)
obj, err := d.ReadObjectAt(offset)
if err != nil {
return nil, err
}
if core.AnyObject != t && obj.Type() != t {
return nil, core.ErrObjectNotFound
}
return obj, nil
}
// Iter returns an iterator for all the objects in the packfile with the
// given type.
func (s *ObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) {
var objects []core.Object
hashes, err := s.dir.Objects()
if err != nil {
return nil, err
}
for _, hash := range hashes {
object, err := s.getFromUnpacked(core.AnyObject, hash)
if err != nil {
return nil, err
}
if object.Type() == t {
objects = append(objects, object)
}
}
for hash := range s.index {
object, err := s.getFromPackfile(core.AnyObject, hash)
if err != nil {
return nil, err
}
if t == core.AnyObject || object.Type() == t {
objects = append(objects, object)
}
}
return core.NewObjectSliceIter(objects), nil
}
func buildIndex(fs fs.Filesystem, dir *dotgit.DotGit) (index.Index, error) {
packs, err := dir.ObjectsPacks()
if err != nil {
return nil, err
}
if len(packs) == 0 {
return nil, nil
}
_, _, err = dir.ObjectPack(packs[0].Name())
if err != nil {
if err == dotgit.ErrIdxNotFound {
return buildIndexFromPackfile(dir)
}
return nil, err
}
return buildIndexFromIdxfile(fs, "")
}
func buildIndexFromPackfile(dir *dotgit.DotGit) (index.Index, error) {
packs, err := dir.ObjectsPacks()
if err != nil {
return nil, err
}
if len(packs) == 0 {
return nil, nil
}
f, _, err := dir.ObjectPack(packs[0].Name())
if err != nil {
return nil, err
}
defer func() {
errClose := f.Close()
if err == nil {
err = errClose
}
}()
index, _, err := index.NewFromPackfile(f)
return index, err
}
func buildIndexFromIdxfile(fs fs.Filesystem, path string) (index.Index, error) {
f, err := fs.Open(path)
if err != nil {
return nil, err
}
defer func() {
errClose := f.Close()
if err == nil {
err = errClose
}
}()
i := index.New()
return i, i.Decode(f)
}
func (o *ObjectStorage) Begin() core.TxObjectStorage {
return &TxObjectStorage{}
}
type TxObjectStorage struct{}
func (tx *TxObjectStorage) Set(obj core.Object) (core.Hash, error) {
return core.ZeroHash, fmt.Errorf("not implemented yet")
}
func (tx *TxObjectStorage) Get(core.ObjectType, core.Hash) (core.Object, error) {
return nil, fmt.Errorf("not implemented yet")
}
func (tx *TxObjectStorage) Commit() error {
return fmt.Errorf("not implemented yet")
}
func (tx *TxObjectStorage) Rollback() error {
return fmt.Errorf("not implemented yet")
}
|