| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/config" |
| 10 |
"github.com/go-git/go-git/v5/plumbing" |
| 11 |
"github.com/go-git/go-git/v5/plumbing/format/index" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 13 |
"github.com/go-git/go-git/v5/storage" |
| 14 |
|
| 15 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 16 |
. "gopkg.in/check.v1" |
| 17 |
) |
| 18 |
|
| 19 |
type Storer interface { |
| 20 |
storer.EncodedObjectStorer |
| 21 |
storer.ReferenceStorer |
| 22 |
storer.ShallowStorer |
| 23 |
storer.IndexStorer |
| 24 |
config.ConfigStorer |
| 25 |
storage.ModuleStorer |
| 26 |
} |
| 27 |
|
| 28 |
type TestObject struct { |
| 29 |
Object plumbing.EncodedObject |
| 30 |
Hash string |
| 31 |
Type plumbing.ObjectType |
| 32 |
} |
| 33 |
|
| 34 |
type BaseStorageSuite struct { |
| 35 |
Storer Storer |
| 36 |
|
| 37 |
validTypes []plumbing.ObjectType |
| 38 |
testObjects map[plumbing.ObjectType]TestObject |
| 39 |
} |
| 40 |
|
| 41 |
func NewBaseStorageSuite(s Storer) BaseStorageSuite { |
| 42 |
commit := &plumbing.MemoryObject{} |
| 43 |
commit.SetType(plumbing.CommitObject) |
| 44 |
tree := &plumbing.MemoryObject{} |
| 45 |
tree.SetType(plumbing.TreeObject) |
| 46 |
blob := &plumbing.MemoryObject{} |
| 47 |
blob.SetType(plumbing.BlobObject) |
| 48 |
tag := &plumbing.MemoryObject{} |
| 49 |
tag.SetType(plumbing.TagObject) |
| 50 |
|
| 51 |
return BaseStorageSuite{ |
| 52 |
Storer: s, |
| 53 |
validTypes: []plumbing.ObjectType{ |
| 54 |
plumbing.CommitObject, |
| 55 |
plumbing.BlobObject, |
| 56 |
plumbing.TagObject, |
| 57 |
plumbing.TreeObject, |
| 58 |
}, |
| 59 |
testObjects: map[plumbing.ObjectType]TestObject{ |
| 60 |
plumbing.CommitObject: {commit, "dcf5b16e76cce7425d0beaef62d79a7d10fce1f5", plumbing.CommitObject}, |
| 61 |
plumbing.TreeObject: {tree, "4b825dc642cb6eb9a060e54bf8d69288fbee4904", plumbing.TreeObject}, |
| 62 |
plumbing.BlobObject: {blob, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", plumbing.BlobObject}, |
| 63 |
plumbing.TagObject: {tag, "d994c6bb648123a17e8f70a966857c546b2a6f94", plumbing.TagObject}, |
| 64 |
}} |
| 65 |
} |
| 66 |
|
| 67 |
func (s *BaseStorageSuite) TearDownTest(c *C) { |
| 68 |
fixtures.Clean() |
| 69 |
} |
| 70 |
|
| 71 |
func (s *BaseStorageSuite) TestSetEncodedObjectAndEncodedObject(c *C) { |
| 72 |
for _, to := range s.testObjects { |
| 73 |
comment := Commentf("failed for type %s", to.Type.String()) |
| 74 |
|
| 75 |
h, err := s.Storer.SetEncodedObject(to.Object) |
| 76 |
c.Assert(err, IsNil) |
| 77 |
c.Assert(h.String(), Equals, to.Hash, comment) |
| 78 |
|
| 79 |
o, err := s.Storer.EncodedObject(to.Type, h) |
| 80 |
c.Assert(err, IsNil) |
| 81 |
c.Assert(objectEquals(o, to.Object), IsNil) |
| 82 |
|
| 83 |
o, err = s.Storer.EncodedObject(plumbing.AnyObject, h) |
| 84 |
c.Assert(err, IsNil) |
| 85 |
c.Assert(objectEquals(o, to.Object), IsNil) |
| 86 |
|
| 87 |
for _, t := range s.validTypes { |
| 88 |
if t == to.Type { |
| 89 |
continue |
| 90 |
} |
| 91 |
|
| 92 |
o, err = s.Storer.EncodedObject(t, h) |
| 93 |
c.Assert(o, IsNil) |
| 94 |
c.Assert(err, Equals, plumbing.ErrObjectNotFound) |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func (s *BaseStorageSuite) TestSetEncodedObjectInvalid(c *C) { |
| 100 |
o := s.Storer.NewEncodedObject() |
| 101 |
o.SetType(plumbing.REFDeltaObject) |
| 102 |
|
| 103 |
_, err := s.Storer.SetEncodedObject(o) |
| 104 |
c.Assert(err, NotNil) |
| 105 |
} |
| 106 |
|
| 107 |
func (s *BaseStorageSuite) TestIterEncodedObjects(c *C) { |
| 108 |
for _, o := range s.testObjects { |
| 109 |
h, err := s.Storer.SetEncodedObject(o.Object) |
| 110 |
c.Assert(err, IsNil) |
| 111 |
c.Assert(h, Equals, o.Object.Hash()) |
| 112 |
} |
| 113 |
|
| 114 |
for _, t := range s.validTypes { |
| 115 |
comment := Commentf("failed for type %s)", t.String()) |
| 116 |
i, err := s.Storer.IterEncodedObjects(t) |
| 117 |
c.Assert(err, IsNil, comment) |
| 118 |
|
| 119 |
o, err := i.Next() |
| 120 |
c.Assert(err, IsNil) |
| 121 |
c.Assert(objectEquals(o, s.testObjects[t].Object), IsNil) |
| 122 |
|
| 123 |
o, err = i.Next() |
| 124 |
c.Assert(o, IsNil) |
| 125 |
c.Assert(err, Equals, io.EOF, comment) |
| 126 |
} |
| 127 |
|
| 128 |
i, err := s.Storer.IterEncodedObjects(plumbing.AnyObject) |
| 129 |
c.Assert(err, IsNil) |
| 130 |
|
| 131 |
foundObjects := []plumbing.EncodedObject{} |
| 132 |
i.ForEach(func(o plumbing.EncodedObject) error { |
| 133 |
foundObjects = append(foundObjects, o) |
| 134 |
return nil |
| 135 |
}) |
| 136 |
|
| 137 |
c.Assert(foundObjects, HasLen, len(s.testObjects)) |
| 138 |
for _, to := range s.testObjects { |
| 139 |
found := false |
| 140 |
for _, o := range foundObjects { |
| 141 |
if to.Object.Hash() == o.Hash() { |
| 142 |
found = true |
| 143 |
break |
| 144 |
} |
| 145 |
} |
| 146 |
c.Assert(found, Equals, true, Commentf("Object of type %s not found", to.Type.String())) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func (s *BaseStorageSuite) TestPackfileWriter(c *C) { |
| 151 |
pwr, ok := s.Storer.(storer.PackfileWriter) |
| 152 |
if !ok { |
| 153 |
c.Skip("not a storer.PackWriter") |
| 154 |
} |
| 155 |
|
| 156 |
pw, err := pwr.PackfileWriter() |
| 157 |
c.Assert(err, IsNil) |
| 158 |
|
| 159 |
f := fixtures.Basic().One() |
| 160 |
_, err = io.Copy(pw, f.Packfile()) |
| 161 |
c.Assert(err, IsNil) |
| 162 |
|
| 163 |
err = pw.Close() |
| 164 |
c.Assert(err, IsNil) |
| 165 |
|
| 166 |
iter, err := s.Storer.IterEncodedObjects(plumbing.AnyObject) |
| 167 |
c.Assert(err, IsNil) |
| 168 |
objects := 0 |
| 169 |
err = iter.ForEach(func(plumbing.EncodedObject) error { |
| 170 |
objects++ |
| 171 |
return nil |
| 172 |
}) |
| 173 |
c.Assert(err, IsNil) |
| 174 |
c.Assert(objects, Equals, 31) |
| 175 |
} |
| 176 |
|
| 177 |
func (s *BaseStorageSuite) TestObjectStorerTxSetEncodedObjectAndCommit(c *C) { |
| 178 |
storer, ok := s.Storer.(storer.Transactioner) |
| 179 |
if !ok { |
| 180 |
c.Skip("not a plumbing.ObjectStorerTx") |
| 181 |
} |
| 182 |
|
| 183 |
tx := storer.Begin() |
| 184 |
for _, o := range s.testObjects { |
| 185 |
h, err := tx.SetEncodedObject(o.Object) |
| 186 |
c.Assert(err, IsNil) |
| 187 |
c.Assert(h.String(), Equals, o.Hash) |
| 188 |
} |
| 189 |
|
| 190 |
iter, err := s.Storer.IterEncodedObjects(plumbing.AnyObject) |
| 191 |
c.Assert(err, IsNil) |
| 192 |
_, err = iter.Next() |
| 193 |
c.Assert(err, Equals, io.EOF) |
| 194 |
|
| 195 |
err = tx.Commit() |
| 196 |
c.Assert(err, IsNil) |
| 197 |
|
| 198 |
iter, err = s.Storer.IterEncodedObjects(plumbing.AnyObject) |
| 199 |
c.Assert(err, IsNil) |
| 200 |
|
| 201 |
var count int |
| 202 |
iter.ForEach(func(o plumbing.EncodedObject) error { |
| 203 |
count++ |
| 204 |
return nil |
| 205 |
}) |
| 206 |
|
| 207 |
c.Assert(count, Equals, 4) |
| 208 |
} |
| 209 |
|
| 210 |
func (s *BaseStorageSuite) TestObjectStorerTxSetObjectAndGetObject(c *C) { |
| 211 |
storer, ok := s.Storer.(storer.Transactioner) |
| 212 |
if !ok { |
| 213 |
c.Skip("not a plumbing.ObjectStorerTx") |
| 214 |
} |
| 215 |
|
| 216 |
tx := storer.Begin() |
| 217 |
for _, expected := range s.testObjects { |
| 218 |
h, err := tx.SetEncodedObject(expected.Object) |
| 219 |
c.Assert(err, IsNil) |
| 220 |
c.Assert(h.String(), Equals, expected.Hash) |
| 221 |
|
| 222 |
o, err := tx.EncodedObject(expected.Type, plumbing.NewHash(expected.Hash)) |
| 223 |
c.Assert(err, IsNil) |
| 224 |
c.Assert(o.Hash().String(), DeepEquals, expected.Hash) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func (s *BaseStorageSuite) TestObjectStorerTxGetObjectNotFound(c *C) { |
| 229 |
storer, ok := s.Storer.(storer.Transactioner) |
| 230 |
if !ok { |
| 231 |
c.Skip("not a plumbing.ObjectStorerTx") |
| 232 |
} |
| 233 |
|
| 234 |
tx := storer.Begin() |
| 235 |
o, err := tx.EncodedObject(plumbing.AnyObject, plumbing.ZeroHash) |
| 236 |
c.Assert(o, IsNil) |
| 237 |
c.Assert(err, Equals, plumbing.ErrObjectNotFound) |
| 238 |
} |
| 239 |
|
| 240 |
func (s *BaseStorageSuite) TestObjectStorerTxSetObjectAndRollback(c *C) { |
| 241 |
storer, ok := s.Storer.(storer.Transactioner) |
| 242 |
if !ok { |
| 243 |
c.Skip("not a plumbing.ObjectStorerTx") |
| 244 |
} |
| 245 |
|
| 246 |
tx := storer.Begin() |
| 247 |
for _, o := range s.testObjects { |
| 248 |
h, err := tx.SetEncodedObject(o.Object) |
| 249 |
c.Assert(err, IsNil) |
| 250 |
c.Assert(h.String(), Equals, o.Hash) |
| 251 |
} |
| 252 |
|
| 253 |
err := tx.Rollback() |
| 254 |
c.Assert(err, IsNil) |
| 255 |
|
| 256 |
iter, err := s.Storer.IterEncodedObjects(plumbing.AnyObject) |
| 257 |
c.Assert(err, IsNil) |
| 258 |
_, err = iter.Next() |
| 259 |
c.Assert(err, Equals, io.EOF) |
| 260 |
} |
| 261 |
|
| 262 |
func (s *BaseStorageSuite) TestSetReferenceAndGetReference(c *C) { |
| 263 |
err := s.Storer.SetReference( |
| 264 |
plumbing.NewReferenceFromStrings("refs/foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), |
| 265 |
) |
| 266 |
c.Assert(err, IsNil) |
| 267 |
|
| 268 |
err = s.Storer.SetReference( |
| 269 |
plumbing.NewReferenceFromStrings("refs/bar", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), |
| 270 |
) |
| 271 |
c.Assert(err, IsNil) |
| 272 |
|
| 273 |
e, err := s.Storer.Reference(plumbing.ReferenceName("refs/foo")) |
| 274 |
c.Assert(err, IsNil) |
| 275 |
c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") |
| 276 |
} |
| 277 |
|
| 278 |
func (s *BaseStorageSuite) TestCheckAndSetReference(c *C) { |
| 279 |
err := s.Storer.SetReference( |
| 280 |
plumbing.NewReferenceFromStrings("refs/foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), |
| 281 |
) |
| 282 |
c.Assert(err, IsNil) |
| 283 |
|
| 284 |
err = s.Storer.CheckAndSetReference( |
| 285 |
plumbing.NewReferenceFromStrings("refs/foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), |
| 286 |
plumbing.NewReferenceFromStrings("refs/foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), |
| 287 |
) |
| 288 |
c.Assert(err, IsNil) |
| 289 |
|
| 290 |
e, err := s.Storer.Reference(plumbing.ReferenceName("refs/foo")) |
| 291 |
c.Assert(err, IsNil) |
| 292 |
c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") |
| 293 |
} |
| 294 |
|
| 295 |
func (s *BaseStorageSuite) TestCheckAndSetReferenceNil(c *C) { |
| 296 |
err := s.Storer.SetReference( |
| 297 |
plumbing.NewReferenceFromStrings("refs/foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), |
| 298 |
) |
| 299 |
c.Assert(err, IsNil) |
| 300 |
|
| 301 |
err = s.Storer.CheckAndSetReference( |
| 302 |
plumbing.NewReferenceFromStrings("refs/foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), |
| 303 |
nil, |
| 304 |
) |
| 305 |
c.Assert(err, IsNil) |
| 306 |
|
| 307 |
e, err := s.Storer.Reference(plumbing.ReferenceName("refs/foo")) |
| 308 |
c.Assert(err, IsNil) |
| 309 |
c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") |
| 310 |
} |
| 311 |
|
| 312 |
func (s *BaseStorageSuite) TestCheckAndSetReferenceError(c *C) { |
| 313 |
err := s.Storer.SetReference( |
| 314 |
plumbing.NewReferenceFromStrings("refs/foo", "c3f4688a08fd86f1bf8e055724c84b7a40a09733"), |
| 315 |
) |
| 316 |
c.Assert(err, IsNil) |
| 317 |
|
| 318 |
err = s.Storer.CheckAndSetReference( |
| 319 |
plumbing.NewReferenceFromStrings("refs/foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), |
| 320 |
plumbing.NewReferenceFromStrings("refs/foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), |
| 321 |
) |
| 322 |
c.Assert(err, Equals, storage.ErrReferenceHasChanged) |
| 323 |
|
| 324 |
e, err := s.Storer.Reference(plumbing.ReferenceName("refs/foo")) |
| 325 |
c.Assert(err, IsNil) |
| 326 |
c.Assert(e.Hash().String(), Equals, "c3f4688a08fd86f1bf8e055724c84b7a40a09733") |
| 327 |
} |
| 328 |
|
| 329 |
func (s *BaseStorageSuite) TestRemoveReference(c *C) { |
| 330 |
err := s.Storer.SetReference( |
| 331 |
plumbing.NewReferenceFromStrings("refs/foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), |
| 332 |
) |
| 333 |
c.Assert(err, IsNil) |
| 334 |
|
| 335 |
err = s.Storer.RemoveReference(plumbing.ReferenceName("refs/foo")) |
| 336 |
c.Assert(err, IsNil) |
| 337 |
|
| 338 |
_, err = s.Storer.Reference(plumbing.ReferenceName("refs/foo")) |
| 339 |
c.Assert(err, Equals, plumbing.ErrReferenceNotFound) |
| 340 |
} |
| 341 |
|
| 342 |
func (s *BaseStorageSuite) TestRemoveReferenceNonExistent(c *C) { |
| 343 |
err := s.Storer.SetReference( |
| 344 |
plumbing.NewReferenceFromStrings("refs/foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), |
| 345 |
) |
| 346 |
c.Assert(err, IsNil) |
| 347 |
|
| 348 |
err = s.Storer.RemoveReference(plumbing.ReferenceName("refs/nonexistent")) |
| 349 |
c.Assert(err, IsNil) |
| 350 |
|
| 351 |
e, err := s.Storer.Reference(plumbing.ReferenceName("refs/foo")) |
| 352 |
c.Assert(err, IsNil) |
| 353 |
c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") |
| 354 |
} |
| 355 |
|
| 356 |
func (s *BaseStorageSuite) TestGetReferenceNotFound(c *C) { |
| 357 |
r, err := s.Storer.Reference(plumbing.ReferenceName("refs/bar")) |
| 358 |
c.Assert(err, Equals, plumbing.ErrReferenceNotFound) |
| 359 |
c.Assert(r, IsNil) |
| 360 |
} |
| 361 |
|
| 362 |
func (s *BaseStorageSuite) TestIterReferences(c *C) { |
| 363 |
err := s.Storer.SetReference( |
| 364 |
plumbing.NewReferenceFromStrings("refs/foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), |
| 365 |
) |
| 366 |
c.Assert(err, IsNil) |
| 367 |
|
| 368 |
i, err := s.Storer.IterReferences() |
| 369 |
c.Assert(err, IsNil) |
| 370 |
|
| 371 |
e, err := i.Next() |
| 372 |
c.Assert(err, IsNil) |
| 373 |
c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") |
| 374 |
|
| 375 |
e, err = i.Next() |
| 376 |
c.Assert(e, IsNil) |
| 377 |
c.Assert(err, Equals, io.EOF) |
| 378 |
} |
| 379 |
|
| 380 |
func (s *BaseStorageSuite) TestSetShallowAndShallow(c *C) { |
| 381 |
expected := []plumbing.Hash{ |
| 382 |
plumbing.NewHash("b66c08ba28aa1f81eb06a1127aa3936ff77e5e2c"), |
| 383 |
plumbing.NewHash("c3f4688a08fd86f1bf8e055724c84b7a40a09733"), |
| 384 |
plumbing.NewHash("c78874f116be67ecf54df225a613162b84cc6ebf"), |
| 385 |
} |
| 386 |
|
| 387 |
err := s.Storer.SetShallow(expected) |
| 388 |
c.Assert(err, IsNil) |
| 389 |
|
| 390 |
result, err := s.Storer.Shallow() |
| 391 |
c.Assert(err, IsNil) |
| 392 |
c.Assert(result, DeepEquals, expected) |
| 393 |
} |
| 394 |
|
| 395 |
func (s *BaseStorageSuite) TestSetConfigAndConfig(c *C) { |
| 396 |
expected := config.NewConfig() |
| 397 |
expected.Core.IsBare = true |
| 398 |
expected.Remotes["foo"] = &config.RemoteConfig{ |
| 399 |
Name: "foo", |
| 400 |
URLs: []string{"http://foo/bar.git"}, |
| 401 |
} |
| 402 |
|
| 403 |
err := s.Storer.SetConfig(expected) |
| 404 |
c.Assert(err, IsNil) |
| 405 |
|
| 406 |
cfg, err := s.Storer.Config() |
| 407 |
c.Assert(err, IsNil) |
| 408 |
|
| 409 |
c.Assert(cfg.Core.IsBare, DeepEquals, expected.Core.IsBare) |
| 410 |
c.Assert(cfg.Remotes, DeepEquals, expected.Remotes) |
| 411 |
} |
| 412 |
|
| 413 |
func (s *BaseStorageSuite) TestIndex(c *C) { |
| 414 |
expected := &index.Index{} |
| 415 |
expected.Version = 2 |
| 416 |
|
| 417 |
idx, err := s.Storer.Index() |
| 418 |
c.Assert(err, IsNil) |
| 419 |
c.Assert(idx, DeepEquals, expected) |
| 420 |
} |
| 421 |
|
| 422 |
func (s *BaseStorageSuite) TestSetIndexAndIndex(c *C) { |
| 423 |
expected := &index.Index{} |
| 424 |
expected.Version = 2 |
| 425 |
|
| 426 |
err := s.Storer.SetIndex(expected) |
| 427 |
c.Assert(err, IsNil) |
| 428 |
|
| 429 |
idx, err := s.Storer.Index() |
| 430 |
c.Assert(err, IsNil) |
| 431 |
|
| 432 |
// ModTime is set during SetIndex (memory storage) or read-back (filesystem storage). |
| 433 |
// Verify it was set. |
| 434 |
c.Assert(idx.ModTime.IsZero(), Equals, false) |
| 435 |
} |
| 436 |
|
| 437 |
func (s *BaseStorageSuite) TestSetConfigInvalid(c *C) { |
| 438 |
cfg := config.NewConfig() |
| 439 |
cfg.Remotes["foo"] = &config.RemoteConfig{} |
| 440 |
|
| 441 |
err := s.Storer.SetConfig(cfg) |
| 442 |
c.Assert(err, NotNil) |
| 443 |
} |
| 444 |
|
| 445 |
func (s *BaseStorageSuite) TestModule(c *C) { |
| 446 |
storer, err := s.Storer.Module("foo") |
| 447 |
c.Assert(err, IsNil) |
| 448 |
c.Assert(storer, NotNil) |
| 449 |
|
| 450 |
storer, err = s.Storer.Module("foo") |
| 451 |
c.Assert(err, IsNil) |
| 452 |
c.Assert(storer, NotNil) |
| 453 |
} |
| 454 |
|
| 455 |
func (s *BaseStorageSuite) TestDeltaObjectStorer(c *C) { |
| 456 |
dos, ok := s.Storer.(storer.DeltaObjectStorer) |
| 457 |
if !ok { |
| 458 |
c.Skip("not an DeltaObjectStorer") |
| 459 |
} |
| 460 |
|
| 461 |
pwr, ok := s.Storer.(storer.PackfileWriter) |
| 462 |
if !ok { |
| 463 |
c.Skip("not a storer.PackWriter") |
| 464 |
} |
| 465 |
|
| 466 |
pw, err := pwr.PackfileWriter() |
| 467 |
c.Assert(err, IsNil) |
| 468 |
|
| 469 |
f := fixtures.Basic().One() |
| 470 |
_, err = io.Copy(pw, f.Packfile()) |
| 471 |
c.Assert(err, IsNil) |
| 472 |
|
| 473 |
err = pw.Close() |
| 474 |
c.Assert(err, IsNil) |
| 475 |
|
| 476 |
h := plumbing.NewHash("32858aad3c383ed1ff0a0f9bdf231d54a00c9e88") |
| 477 |
obj, err := dos.DeltaObject(plumbing.AnyObject, h) |
| 478 |
c.Assert(err, IsNil) |
| 479 |
c.Assert(obj.Type(), Equals, plumbing.BlobObject) |
| 480 |
|
| 481 |
h = plumbing.NewHash("aa9b383c260e1d05fbbf6b30a02914555e20c725") |
| 482 |
obj, err = dos.DeltaObject(plumbing.AnyObject, h) |
| 483 |
c.Assert(err, IsNil) |
| 484 |
c.Assert(obj.Type(), Equals, plumbing.OFSDeltaObject) |
| 485 |
_, ok = obj.(plumbing.DeltaObject) |
| 486 |
c.Assert(ok, Equals, true) |
| 487 |
} |
| 488 |
|
| 489 |
func objectEquals(a plumbing.EncodedObject, b plumbing.EncodedObject) error { |
| 490 |
ha := a.Hash() |
| 491 |
hb := b.Hash() |
| 492 |
if ha != hb { |
| 493 |
return fmt.Errorf("hashes do not match: %s != %s", |
| 494 |
ha.String(), hb.String()) |
| 495 |
} |
| 496 |
|
| 497 |
ra, err := a.Reader() |
| 498 |
if err != nil { |
| 499 |
return fmt.Errorf("can't get reader on a: %q", err) |
| 500 |
} |
| 501 |
|
| 502 |
rb, err := b.Reader() |
| 503 |
if err != nil { |
| 504 |
return fmt.Errorf("can't get reader on b: %q", err) |
| 505 |
} |
| 506 |
|
| 507 |
ca, err := io.ReadAll(ra) |
| 508 |
if err != nil { |
| 509 |
return fmt.Errorf("error reading a: %q", err) |
| 510 |
} |
| 511 |
|
| 512 |
cb, err := io.ReadAll(rb) |
| 513 |
if err != nil { |
| 514 |
return fmt.Errorf("error reading b: %q", err) |
| 515 |
} |
| 516 |
|
| 517 |
if hex.EncodeToString(ca) != hex.EncodeToString(cb) { |
| 518 |
return errors.New("content does not match") |
| 519 |
} |
| 520 |
|
| 521 |
err = rb.Close() |
| 522 |
if err != nil { |
| 523 |
return fmt.Errorf("can't close reader on b: %q", err) |
| 524 |
} |
| 525 |
|
| 526 |
err = ra.Close() |
| 527 |
if err != nil { |
| 528 |
return fmt.Errorf("can't close reader on a: %q", err) |
| 529 |
} |
| 530 |
|
| 531 |
return nil |
| 532 |
} |