Esempio n. 1
0
func (w *PackWriter) buildIndex() {
	s := packfile.NewScanner(w.synced)
	d, err := packfile.NewDecoder(s, nil)
	if err != nil {
		w.result <- err
		return
	}

	checksum, err := d.Decode()
	if err != nil {
		w.result <- err
		return
	}

	w.checksum = checksum
	w.index.PackfileChecksum = checksum
	w.index.Version = idxfile.VersionSupported

	offsets := d.Offsets()
	for h, crc := range d.CRCs() {
		w.index.Add(h, uint64(offsets[h]), crc)
	}

	w.result <- err
}
Esempio n. 2
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. 3
0
func unpackFixtures(c *C, fixtures ...[]packedFixture) map[string]*Repository {
	repos := make(map[string]*Repository, 0)
	for _, group := range fixtures {
		for _, fixture := range group {
			if _, existing := repos[fixture.url]; existing {
				continue
			}

			comment := Commentf("fixture packfile: %q", fixture.packfile)

			repos[fixture.url] = NewMemoryRepository()

			f, err := os.Open(fixture.packfile)
			c.Assert(err, IsNil, comment)

			r := packfile.NewScanner(f)
			d, err := packfile.NewDecoder(r, repos[fixture.url].s)
			c.Assert(err, IsNil, comment)
			_, err = d.Decode()
			c.Assert(err, IsNil, comment)
			c.Assert(f.Close(), IsNil, comment)
		}
	}

	return repos
}
Esempio n. 4
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. 5
0
func (s *ReaderSuite) TestNewDecodeNonSeekable(c *C) {
	scanner := packfile.NewScanner(nil)
	d, err := packfile.NewDecoder(scanner, nil)

	c.Assert(d, IsNil)
	c.Assert(err, NotNil)
}
Esempio n. 6
0
func (s *BaseSuite) NewRepositoryFromPackfile(f *fixtures.Fixture) *Repository {
	h := f.PackfileHash.String()
	if r, ok := s.cache[h]; ok {
		return r
	}

	r := NewMemoryRepository()

	p := f.Packfile()
	defer p.Close()

	n := packfile.NewScanner(p)
	d, err := packfile.NewDecoder(n, r.s)
	if err != nil {
		panic(err)
	}

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

	s.cache[h] = r
	return r
}
Esempio n. 7
0
func (s *ReaderSuite) TestDecodeInMemory(c *C) {
	fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
		scanner := packfile.NewScanner(f.Packfile())
		d, err := packfile.NewDecoder(scanner, nil)
		c.Assert(err, IsNil)

		ch, err := d.Decode()
		c.Assert(err, IsNil)
		c.Assert(ch, Equals, f.PackfileHash)
	})
}
Esempio n. 8
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. 9
0
func (s *ReaderSuite) TestOffsets(c *C) {
	f := fixtures.Basic().One()
	scanner := packfile.NewScanner(f.Packfile())
	d, err := packfile.NewDecoder(scanner, nil)
	c.Assert(err, IsNil)

	c.Assert(d.Offsets(), HasLen, 0)

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

	c.Assert(d.Offsets(), HasLen, 31)
}
Esempio n. 10
0
func (s *ReaderSuite) TestSetOffsets(c *C) {
	f := fixtures.Basic().One()
	scanner := packfile.NewScanner(f.Packfile())
	d, err := packfile.NewDecoder(scanner, nil)
	c.Assert(err, IsNil)

	h := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
	d.SetOffsets(map[plumbing.Hash]int64{h: 42})

	o := d.Offsets()
	c.Assert(o, HasLen, 1)
	c.Assert(o[h], Equals, int64(42))
}
Esempio n. 11
0
func (s *BaseSuite) buildRepositories(c *C, f fixtures.Fixtures) {
	for _, fixture := range f {
		r := NewMemoryRepository()

		f := fixture.Packfile()
		defer f.Close()

		n := packfile.NewScanner(f)
		d, err := packfile.NewDecoder(n, r.s)
		c.Assert(err, IsNil)
		_, err = d.Decode()
		c.Assert(err, IsNil)

		s.Repositories[fixture.URL] = r
	}
}
Esempio n. 12
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. 13
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. 14
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. 15
0
func (s *ReaderSuite) TestReadObjectAt(c *C) {
	f := fixtures.Basic().One()
	scanner := packfile.NewScanner(f.Packfile())
	d, err := packfile.NewDecoder(scanner, nil)
	c.Assert(err, IsNil)

	// when the packfile is ref-delta based, the offsets are required
	if f.Is("ref-delta") {
		offsets := getOffsetsFromIdx(f.Idx())
		d.SetOffsets(offsets)
	}

	// the objects at reference 186, is a delta, so should be recall,
	// without being read before.
	obj, err := d.DecodeObjectAt(186)
	c.Assert(err, IsNil)
	c.Assert(obj.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
}
Esempio n. 16
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. 17
0
func (r *Remote) updateObjectStorage(reader io.Reader) error {
	if sw, ok := r.s.(storer.PackfileWriter); ok {
		w, err := sw.PackfileWriter()
		if err != nil {
			return err
		}

		defer w.Close()
		_, err = io.Copy(w, reader)
		return err
	}

	stream := packfile.NewScanner(reader)
	d, err := packfile.NewDecoder(stream, r.s)
	if err != nil {
		return err
	}

	_, err = d.Decode()
	return err
}
Esempio n. 18
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. 19
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)
	})
}