22package core
33
44import (
5- "bytes"
65 "errors"
76 "io"
87)
@@ -13,11 +12,11 @@ var (
1312
1413// Object is a generic representation of any git object
1514type Object interface {
15+ Hash () Hash
1616 Type () ObjectType
1717 SetType (ObjectType )
1818 Size () int64
1919 SetSize (int64 )
20- Hash () Hash
2120 Reader () io.Reader
2221 Writer () io.Writer
2322}
@@ -147,87 +146,3 @@ func (iter *ObjectSliceIter) Next() (Object, error) {
147146func (iter * ObjectSliceIter ) Close () {
148147 iter .pos = len (iter .series )
149148}
150-
151- type RAWObject struct {
152- b []byte
153- t ObjectType
154- s int64
155- }
156-
157- func (o * RAWObject ) Type () ObjectType { return o .t }
158- func (o * RAWObject ) SetType (t ObjectType ) { o .t = t }
159- func (o * RAWObject ) Size () int64 { return o .s }
160- func (o * RAWObject ) SetSize (s int64 ) { o .s = s }
161- func (o * RAWObject ) Reader () io.Reader { return bytes .NewBuffer (o .b ) }
162- func (o * RAWObject ) Hash () Hash { return ComputeHash (o .t , o .b ) }
163- func (o * RAWObject ) Writer () io.Writer { return o }
164- func (o * RAWObject ) Write (p []byte ) (n int , err error ) {
165- o .b = append (o .b , p ... )
166- return len (p ), nil
167- }
168-
169- type RAWObjectStorage struct {
170- Objects map [Hash ]Object
171- Commits map [Hash ]Object
172- Trees map [Hash ]Object
173- Blobs map [Hash ]Object
174- }
175-
176- func NewRAWObjectStorage () * RAWObjectStorage {
177- return & RAWObjectStorage {
178- Objects : make (map [Hash ]Object , 0 ),
179- Commits : make (map [Hash ]Object , 0 ),
180- Trees : make (map [Hash ]Object , 0 ),
181- Blobs : make (map [Hash ]Object , 0 ),
182- }
183- }
184-
185- func (o * RAWObjectStorage ) New () (Object , error ) {
186- return & RAWObject {}, nil
187- }
188-
189- func (o * RAWObjectStorage ) Set (obj Object ) (Hash , error ) {
190- h := obj .Hash ()
191- o .Objects [h ] = obj
192-
193- switch obj .Type () {
194- case CommitObject :
195- o .Commits [h ] = o .Objects [h ]
196- case TreeObject :
197- o .Trees [h ] = o .Objects [h ]
198- case BlobObject :
199- o .Blobs [h ] = o .Objects [h ]
200- }
201-
202- return h , nil
203- }
204-
205- func (o * RAWObjectStorage ) Get (h Hash ) (Object , error ) {
206- obj , ok := o .Objects [h ]
207- if ! ok {
208- return nil , ObjectNotFoundErr
209- }
210-
211- return obj , nil
212- }
213-
214- func (o * RAWObjectStorage ) Iter (t ObjectType ) ObjectIter {
215- var series []Object
216- switch t {
217- case CommitObject :
218- series = flattenObjectMap (o .Commits )
219- case TreeObject :
220- series = flattenObjectMap (o .Trees )
221- case BlobObject :
222- series = flattenObjectMap (o .Blobs )
223- }
224- return NewObjectSliceIter (series )
225- }
226-
227- func flattenObjectMap (m map [Hash ]Object ) []Object {
228- objects := make ([]Object , 0 , len (m ))
229- for _ , obj := range m {
230- objects = append (objects , obj )
231- }
232- return objects
233- }
0 commit comments