Esempio n. 1
0
func (s *EncoderSuite) simpleDeltaTest(c *C) {
	srcObject := newObject(plumbing.BlobObject, []byte("0"))
	targetObject := newObject(plumbing.BlobObject, []byte("01"))

	deltaObject, err := GetDelta(srcObject, targetObject)
	c.Assert(err, IsNil)

	srcToPack := newObjectToPack(srcObject)
	_, err = s.enc.encode([]*ObjectToPack{
		srcToPack,
		newDeltaObjectToPack(srcToPack, targetObject, deltaObject),
	})
	c.Assert(err, IsNil)

	scanner := NewScanner(s.buf)

	storage := memory.NewStorage()
	d, err := NewDecoder(scanner, storage)
	c.Assert(err, IsNil)

	_, err = d.Decode()
	c.Assert(err, IsNil)

	decSrc, err := storage.EncodedObject(srcObject.Type(), srcObject.Hash())
	c.Assert(err, IsNil)
	c.Assert(decSrc, DeepEquals, srcObject)

	decTarget, err := storage.EncodedObject(targetObject.Type(), targetObject.Hash())
	c.Assert(err, IsNil)
	c.Assert(decTarget, DeepEquals, targetObject)
}
Esempio n. 2
0
func (s *RemoteSuite) TestFetch(c *C) {
	sto := memory.NewStorage()
	r := newRemote(sto, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture})
	r.upSrv = &MockGitUploadPackService{}

	c.Assert(r.Connect(), IsNil)

	err := r.Fetch(&FetchOptions{
		RefSpecs: []config.RefSpec{FixRefSpec},
	})

	c.Assert(err, IsNil)
	c.Assert(sto.Objects, HasLen, 31)

	expectedRefs := []*plumbing.Reference{
		plumbing.NewReferenceFromStrings("refs/remotes/origin/master", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
		plumbing.NewReferenceFromStrings("refs/remotes/origin/branch", "e8d3ffab552895c19b9fcf7aa264d277cde33881"),
		plumbing.NewReferenceFromStrings("refs/tags/v1.0.0", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
	}

	for _, exp := range expectedRefs {
		r, _ := sto.Reference(exp.Name())
		c.Assert(exp.String(), Equals, r.String())
	}
}
Esempio n. 3
0
func (s *IdxfileSuite) TestDecodeCRCs(c *C) {
	f := fixtures.Basic().ByTag("ofs-delta").One()

	scanner := packfile.NewScanner(f.Packfile())
	storage := memory.NewStorage()

	pd, err := packfile.NewDecoder(scanner, storage)
	c.Assert(err, IsNil)
	_, err = pd.Decode()
	c.Assert(err, IsNil)

	i := &Idxfile{Version: VersionSupported}

	offsets := pd.Offsets()
	for h, crc := range pd.CRCs() {
		i.Add(h, uint64(offsets[h]), crc)
	}

	buf := bytes.NewBuffer(nil)
	e := NewEncoder(buf)
	_, err = e.Encode(i)
	c.Assert(err, IsNil)

	idx := &Idxfile{}

	d := NewDecoder(buf)
	err = d.Decode(idx)
	c.Assert(err, IsNil)

	c.Assert(idx.Entries, DeepEquals, i.Entries)
}
Esempio n. 4
0
func (s *RemoteSuite) TestFetchDepth(c *C) {
	url := s.GetBasicLocalRepositoryURL()
	sto := memory.NewStorage()
	r := newRemote(sto, nil, &config.RemoteConfig{Name: "foo", URL: url})

	refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
	err := r.Fetch(&FetchOptions{
		RefSpecs: []config.RefSpec{refspec},
		Depth:    1,
	})

	c.Assert(err, IsNil)
	c.Assert(sto.Objects, HasLen, 18)

	expectedRefs := []*plumbing.Reference{
		plumbing.NewReferenceFromStrings("refs/remotes/origin/master", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
		plumbing.NewReferenceFromStrings("refs/remotes/origin/branch", "e8d3ffab552895c19b9fcf7aa264d277cde33881"),
	}

	for _, exp := range expectedRefs {
		r, _ := sto.Reference(exp.Name())
		c.Assert(exp.String(), Equals, r.String())
	}

	h, err := sto.Shallow()
	c.Assert(err, IsNil)
	c.Assert(h, HasLen, 2)
	c.Assert(h, DeepEquals, []plumbing.Hash{
		plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881"),
		plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
	})
}
Esempio n. 5
0
func (s *DiffTreeSuite) storageFromPackfile(f *fixtures.Fixture) storer.EncodedObjectStorer {
	sto, ok := s.cache[f.URL]
	if ok {
		return sto
	}

	sto = memory.NewStorage()

	pf := f.Packfile()

	defer pf.Close()

	n := packfile.NewScanner(pf)
	d, err := packfile.NewDecoder(n, sto)
	if err != nil {
		panic(err)
	}

	_, err = d.Decode()
	if err != nil {
		panic(err)
	}

	s.cache[f.URL] = sto
	return sto
}
Esempio n. 6
0
func (s *SendPackSuite) emptyPackfile() io.ReadCloser {
	var buf bytes.Buffer
	e := packfile.NewEncoder(&buf, memory.NewStorage(), false)
	_, err := e.Encode(nil)
	if err != nil {
		panic(err)
	}

	return ioutil.NopCloser(&buf)
}
Esempio n. 7
0
func (s *FetchPackSuite) checkObjectNumber(c *C, r io.Reader, n int) {
	b, err := ioutil.ReadAll(r)
	c.Assert(err, IsNil)
	buf := bytes.NewBuffer(b)
	scanner := packfile.NewScanner(buf)
	storage := memory.NewStorage()
	d, err := packfile.NewDecoder(scanner, storage)
	c.Assert(err, IsNil)
	_, err = d.Decode()
	c.Assert(err, IsNil)
	c.Assert(len(storage.Objects), Equals, n)
}
Esempio n. 8
0
func (s *RemoteSuite) TestFetchNoErrAlreadyUpToDate(c *C) {
	url := s.GetBasicLocalRepositoryURL()
	sto := memory.NewStorage()
	r := newRemote(sto, nil, &config.RemoteConfig{Name: "foo", URL: url})

	refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
	o := &FetchOptions{
		RefSpecs: []config.RefSpec{refspec},
	}

	err := r.Fetch(o)
	c.Assert(err, IsNil)
	err = r.Fetch(o)
	c.Assert(err, Equals, NoErrAlreadyUpToDate)
}
Esempio n. 9
0
func (s *RemoteSuite) TestFetchNoErrAlreadyUpToDate(c *C) {
	sto := memory.NewStorage()
	r := newRemote(sto, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture})
	r.upSrv = &MockGitUploadPackService{}

	c.Assert(r.Connect(), IsNil)

	o := &FetchOptions{
		RefSpecs: []config.RefSpec{FixRefSpec},
	}

	err := r.Fetch(o)
	c.Assert(err, IsNil)
	err = r.Fetch(o)
	c.Assert(err, Equals, NoErrAlreadyUpToDate)
}
Esempio n. 10
0
func (s *ReaderSuite) TestDecode(c *C) {
	fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
		scanner := packfile.NewScanner(f.Packfile())
		storage := memory.NewStorage()

		d, err := packfile.NewDecoder(scanner, storage)
		c.Assert(err, IsNil)
		defer d.Close()

		ch, err := d.Decode()
		c.Assert(err, IsNil)
		c.Assert(ch, Equals, f.PackfileHash)

		assertObjects(c, storage, expectedHashes)
	})
}
Esempio n. 11
0
func (s *RemoteSuite) TestFetchWithProgress(c *C) {
	url := s.GetBasicLocalRepositoryURL()
	sto := memory.NewStorage()
	buf := bytes.NewBuffer(nil)

	r := newRemote(sto, buf, &config.RemoteConfig{Name: "foo", URL: url})

	refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
	err := r.Fetch(&FetchOptions{
		RefSpecs: []config.RefSpec{refspec},
	})

	c.Assert(err, IsNil)
	c.Assert(sto.Objects, HasLen, 31)

	c.Assert(buf.Len(), Not(Equals), 0)
}
Esempio n. 12
0
func (s *ReaderSuite) TestDecodeMultipleTimes(c *C) {
	f := fixtures.Basic().ByTag("packfile").One()
	scanner := packfile.NewScanner(f.Packfile())
	storage := memory.NewStorage()

	d, err := packfile.NewDecoder(scanner, storage)
	c.Assert(err, IsNil)
	defer d.Close()

	ch, err := d.Decode()
	c.Assert(err, IsNil)
	c.Assert(ch, Equals, f.PackfileHash)

	ch, err = d.Decode()
	c.Assert(err, Equals, packfile.ErrAlreadyDecoded)
	c.Assert(ch, Equals, plumbing.ZeroHash)
}
Esempio n. 13
0
func (s *ReaderSuite) TestDecodeCRCs(c *C) {
	f := fixtures.Basic().ByTag("ofs-delta").One()

	scanner := packfile.NewScanner(f.Packfile())
	storage := memory.NewStorage()

	d, err := packfile.NewDecoder(scanner, storage)
	c.Assert(err, IsNil)
	_, err = d.Decode()
	c.Assert(err, IsNil)

	var sum uint64
	for _, crc := range d.CRCs() {
		sum += uint64(crc)
	}

	c.Assert(int(sum), Equals, 78022211966)
}
Esempio n. 14
0
func newPackfileIter(f billy.File, t plumbing.ObjectType, seen map[plumbing.Hash]bool) (storer.EncodedObjectIter, error) {
	s := packfile.NewScanner(f)
	_, total, err := s.Header()
	if err != nil {
		return nil, err
	}

	d, err := packfile.NewDecoder(s, memory.NewStorage())
	if err != nil {
		return nil, err
	}

	return &packfileIter{
		f: f,
		d: d,
		t: t,

		total: total,
		seen:  seen,
	}, nil
}
Esempio n. 15
0
// Get returns the object with the given hash, by searching for it in
// the packfile.
func (s *ObjectStorage) getFromPackfile(h plumbing.Hash) (plumbing.EncodedObject, error) {
	pack, offset := s.findObjectInPackfile(h)
	if offset == -1 {
		return nil, plumbing.ErrObjectNotFound
	}

	f, err := s.dir.ObjectPack(pack)
	if err != nil {
		return nil, err
	}

	defer f.Close()

	p := packfile.NewScanner(f)
	d, err := packfile.NewDecoder(p, memory.NewStorage())
	if err != nil {
		return nil, err
	}

	d.SetOffsets(s.index[pack])
	return d.DecodeObjectAt(offset)
}
Esempio n. 16
0
func (s *ReaderSuite) TestDecodeNoSeekableWithTxStorer(c *C) {
	fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
		reader := nonSeekableReader{
			r: f.Packfile(),
		}

		scanner := packfile.NewScanner(reader)

		var storage storer.EncodedObjectStorer = memory.NewStorage()
		_, isTxStorer := storage.(storer.Transactioner)
		c.Assert(isTxStorer, Equals, true)

		d, err := packfile.NewDecoder(scanner, storage)
		c.Assert(err, IsNil)
		defer d.Close()

		ch, err := d.Decode()
		c.Assert(err, IsNil)
		c.Assert(ch, Equals, f.PackfileHash)

		assertObjects(c, storage, expectedHashes)
	})
}
Esempio n. 17
0
func (s *TagSuite) TestTagToTagString(c *C) {
	store := memory.NewStorage()

	tagOneHash := plumbing.NewHash("TAGONE")
	tagTwoHash := plumbing.NewHash("TAGTWO")

	tagOne := &Tag{
		Target:     tagTwoHash,
		Hash:       tagOneHash,
		Name:       "TAG ONE",
		TargetType: plumbing.TagObject,
	}
	tagTwo := &Tag{
		Target:     tagOneHash,
		Hash:       tagTwoHash,
		Name:       "TAG TWO",
		TargetType: plumbing.TagObject,
	}
	oOne := &plumbing.MemoryObject{}
	tagOne.Encode(oOne)
	oTwo := &plumbing.MemoryObject{}
	tagTwo.Encode(oTwo)

	store.SetEncodedObject(oOne)
	store.SetEncodedObject(oTwo)

	tag, err := GetTag(store, tagOneHash)
	c.Assert(err, IsNil)

	c.Assert(tag.String(), Equals,
		"tag TAG TWO\n"+
			"Tagger:  <>\n"+
			"Date:   Mon Jan 01 00:00:00 0001 +0000\n"+
			"\n"+
			"\n")
}
Esempio n. 18
0
func (s *DeltaSelectorSuite) SetUpTest(c *C) {
	s.store = memory.NewStorage()
	s.createTestObjects()
	s.ds = newDeltaSelector(s.store)
}
Esempio n. 19
0
// NewMemoryRepository creates a new repository, backed by a memory.Storage
func NewMemoryRepository() *Repository {
	r, _ := NewRepository(memory.NewStorage())
	return r
}
Esempio n. 20
0
func (s *EncoderSuite) TestDecodeEncodeDecode(c *C) {
	fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
		scanner := NewScanner(f.Packfile())
		storage := memory.NewStorage()

		d, err := NewDecoder(scanner, storage)
		c.Assert(err, IsNil)

		ch, err := d.Decode()
		c.Assert(err, IsNil)
		c.Assert(ch, Equals, f.PackfileHash)

		objIter, err := d.o.IterEncodedObjects(plumbing.AnyObject)
		c.Assert(err, IsNil)

		objects := []plumbing.EncodedObject{}
		hashes := []plumbing.Hash{}
		err = objIter.ForEach(func(o plumbing.EncodedObject) error {
			objects = append(objects, o)
			hash, err := s.store.SetEncodedObject(o)
			c.Assert(err, IsNil)

			hashes = append(hashes, hash)

			return err

		})
		c.Assert(err, IsNil)
		_, err = s.enc.Encode(hashes)
		c.Assert(err, IsNil)

		scanner = NewScanner(s.buf)
		storage = memory.NewStorage()
		d, err = NewDecoder(scanner, storage)
		c.Assert(err, IsNil)
		_, err = d.Decode()
		c.Assert(err, IsNil)

		objIter, err = d.o.IterEncodedObjects(plumbing.AnyObject)
		c.Assert(err, IsNil)
		obtainedObjects := []plumbing.EncodedObject{}
		err = objIter.ForEach(func(o plumbing.EncodedObject) error {
			obtainedObjects = append(obtainedObjects, o)

			return nil
		})
		c.Assert(err, IsNil)
		c.Assert(len(obtainedObjects), Equals, len(objects))

		equals := 0
		for _, oo := range obtainedObjects {
			for _, o := range objects {
				if o.Hash() == oo.Hash() {
					equals++
				}
			}
		}

		c.Assert(len(obtainedObjects), Equals, equals)
	})
}
Esempio n. 21
0
func (s *EncoderSuite) SetUpTest(c *C) {
	s.buf = bytes.NewBuffer(nil)
	s.store = memory.NewStorage()
	s.enc = NewEncoder(s.buf, s.store, false)
}