This repository was archived by the owner on Sep 11, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -133,6 +133,15 @@ func (o *MockObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbi
133133 return plumbing .ZeroHash , nil
134134}
135135
136+ func (o * MockObjectStorage ) HasEncodedObject (h plumbing.Hash ) error {
137+ for _ , o := range o .db {
138+ if o .Hash () == h {
139+ return nil
140+ }
141+ }
142+ return plumbing .ErrObjectNotFound
143+ }
144+
136145func (o * MockObjectStorage ) EncodedObject (t plumbing.ObjectType , h plumbing.Hash ) (plumbing.EncodedObject , error ) {
137146 for _ , o := range o .db {
138147 if o .Hash () == h {
Original file line number Diff line number Diff line change @@ -126,6 +126,32 @@ func (s *ObjectStorage) SetEncodedObject(o plumbing.EncodedObject) (plumbing.Has
126126 return o .Hash (), err
127127}
128128
129+ // HasEncodedObject returns nil if the object exists, without actually
130+ // reading the object data from storage.
131+ func (s * ObjectStorage ) HasEncodedObject (h plumbing.Hash ) (err error ) {
132+ // Check unpacked objects
133+ f , err := s .dir .Object (h )
134+ if err != nil {
135+ if ! os .IsNotExist (err ) {
136+ return err
137+ }
138+ // Fall through to check packed objects.
139+ } else {
140+ defer ioutil .CheckClose (f , & err )
141+ return nil
142+ }
143+
144+ // Check packed objects.
145+ if err := s .requireIndex (); err != nil {
146+ return err
147+ }
148+ _ , _ , offset := s .findObjectInPackfile (h )
149+ if offset == - 1 {
150+ return plumbing .ErrObjectNotFound
151+ }
152+ return nil
153+ }
154+
129155// EncodedObject returns the object with the given hash, by searching for it in
130156// the packfile and the git object directories.
131157func (s * ObjectStorage ) EncodedObject (t plumbing.ObjectType , h plumbing.Hash ) (plumbing.EncodedObject , error ) {
Original file line number Diff line number Diff line change @@ -115,6 +115,14 @@ func (o *ObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.H
115115 return h , nil
116116}
117117
118+ func (o * ObjectStorage ) HasEncodedObject (h plumbing.Hash ) (err error ) {
119+ _ , ok := o .Objects [h ]
120+ if ! ok {
121+ return plumbing .ErrObjectNotFound
122+ }
123+ return nil
124+ }
125+
118126func (o * ObjectStorage ) EncodedObject (t plumbing.ObjectType , h plumbing.Hash ) (plumbing.EncodedObject , error ) {
119127 obj , ok := o .Objects [h ]
120128 if ! ok || (plumbing .AnyObject != t && obj .Type () != t ) {
You can’t perform that action at this time.
0 commit comments