// Set adds a new object to the storage. func (s *ObjectStorage) SetObject(o plumbing.Object) (plumbing.Hash, error) { if o.Type() == plumbing.OFSDeltaObject || o.Type() == plumbing.REFDeltaObject { return plumbing.ZeroHash, plumbing.ErrInvalidType } ow, err := s.dir.NewObject() if err != nil { return plumbing.ZeroHash, err } defer ow.Close() or, err := o.Reader() if err != nil { return plumbing.ZeroHash, err } defer or.Close() if err := ow.WriteHeader(o.Type(), o.Size()); err != nil { return plumbing.ZeroHash, err } if _, err := io.Copy(ow, or); err != nil { return plumbing.ZeroHash, err } return o.Hash(), nil }
// Decode transforms a plumbing.Object into a Blob struct. func (b *Blob) Decode(o plumbing.Object) error { if o.Type() != plumbing.BlobObject { return ErrUnsupportedObject } b.Hash = o.Hash() b.Size = o.Size() b.obj = o return nil }
// Decode transforms a plumbing.Object into a Tag struct. func (t *Tag) Decode(o plumbing.Object) (err error) { if o.Type() != plumbing.TagObject { return ErrUnsupportedObject } t.Hash = o.Hash() reader, err := o.Reader() if err != nil { return err } defer checkClose(reader, &err) r := bufio.NewReader(reader) for { line, err := r.ReadSlice('\n') if err != nil && err != io.EOF { return err } line = bytes.TrimSpace(line) if len(line) == 0 { break // Start of message } split := bytes.SplitN(line, []byte{' '}, 2) switch string(split[0]) { case "object": t.Target = plumbing.NewHash(string(split[1])) case "type": t.TargetType, err = plumbing.ParseObjectType(string(split[1])) if err != nil { return err } case "tag": t.Name = string(split[1]) case "tagger": t.Tagger.Decode(split[1]) } if err == io.EOF { return nil } } data, err := ioutil.ReadAll(r) if err != nil { return err } t.Message = string(data) return nil }
func (o *ObjectStorage) SetObject(obj plumbing.Object) (plumbing.Hash, error) { h := obj.Hash() o.Objects[h] = obj switch obj.Type() { case plumbing.CommitObject: o.Commits[h] = o.Objects[h] case plumbing.TreeObject: o.Trees[h] = o.Objects[h] case plumbing.BlobObject: o.Blobs[h] = o.Objects[h] case plumbing.TagObject: o.Tags[h] = o.Objects[h] default: return h, ErrUnsupportedObjectType } return h, nil }
func (tx *TxObjectStorage) SetObject(obj plumbing.Object) (plumbing.Hash, error) { h := obj.Hash() tx.Objects[h] = obj return h, nil }
func (s *Storage) SetObject(obj plumbing.Object) (plumbing.Hash, error) { key, err := s.buildKey(obj.Hash(), obj.Type()) if err != nil { return obj.Hash(), err } r, err := obj.Reader() if err != nil { return obj.Hash(), err } c, err := ioutil.ReadAll(r) if err != nil { return obj.Hash(), err } bins := driver.BinMap{ urlField: s.url, "hash": obj.Hash().String(), "type": obj.Type().String(), "blob": c, } err = s.client.Put(nil, key, bins) return obj.Hash(), err }