Ejemplo n.º 1
0
// parseAndCompareTree will take a string tree representation and compare
// it with that tree from the provided repository. A tree representation
// has the following format:
//
//     <oid_of_this_tree><LF>
//     <size><LF>
//     <output_of_cat_file_p>
//
// Example:
//
// `dc5c0113b3d246da13b9d4b54861d2894da45e5a
// 348
// 100644 blob 022f0ad6583f99c3c01d0245db452e58bbb8fc8a	.gitignore
// 100644 blob 4760db634060e0f5c76eddb18031d53bff882a23	README
// 040000 tree 888fd8c9a6612c01d8f7c4e71fef114c3d3386d5	api
// 040000 tree 7ba590c4aa02ba2268f732f70b6143cd60457782	builtin
// 040000 tree bd52eecc257922f4c9008d080ad7dd5f9ac3444e	dotfiles
// 040000 tree 9ee2de084ec91660183740dacd8988c8170537e3	ggcase
// 100644 blob 82a1896a6d17e56220681fb66f94ac8a7e1d9bdc	ggit.go
// 100644 blob 3b20433fdf8544b2cff26989c5fc63327137e809	templates.go
// 040000 tree d42d8e7cf260b951e17687404031b1975ffa0b38	test
// 100644 blob 0c01aeb4856324d8487a252e32e04961bf603fad	version.go
// `
func parseAndCompareTree(t *testing.T, repo Repository, treeRepr string) {
	p := parse.ObjectParserForString(treeRepr)

	oid := p.ParseOid()
	p.ConsumeByte(token.LF)
	size := p.ParseInt(token.LF, 10, 32)

	o, err := repo.ObjectFromOid(oid)
	util.AssertNoErr(t, err)

	hdr := o.Header()
	util.Assert(t, hdr.Type() == objects.ObjectTree)
	util.Assert(t, hdr.Size() == size)

	tree := o.(*objects.Tree)
	entries := tree.Entries()

	for !p.EOF() {
		mode := p.ParseFileMode(token.SP)
		otype := objects.ObjectType(p.ConsumeStrings(token.ObjectTypes))
		p.ConsumeByte(token.SP)
		oidStr := p.ParseOid().String()
		p.ConsumeByte(token.TAB)
		name := p.ReadString(token.LF)
		util.Assert(t, len(entries) > 0)
		entry := entries[0]
		util.Assert(t, mode == entry.Mode())
		util.Assertf(t, name == entry.Name(), "expecting: `%s` got: `%s", name, entry.Name())
		util.Assert(t, otype == entry.ObjectType())
		util.Assertf(t, oidStr == entry.ObjectId().String(), "expecting: `%s` got: `%s", oidStr, entry.ObjectId())
		entries = entries[1:]
	}
	util.Assert(t, len(entries) == 0)
}
Ejemplo n.º 2
0
Archivo: tags.go Proyecto: jbrukh/ggit
func (p *objectParser) parseTag() *objects.Tag {
	p.ResetCount()

	// read the object id
	p.ConsumeString(markerObject)
	p.ConsumeByte(token.SP)
	target := p.ParseOid()
	p.ConsumeByte(token.LF)

	// read object type
	p.ConsumeString(markerType)
	p.ConsumeByte(token.SP)
	t := objects.ObjectType(p.ConsumeStrings(token.ObjectTypes))
	p.ConsumeByte(token.LF)

	// read the tag name
	p.ConsumeString(markerTag)
	p.ConsumeByte(token.SP)
	name := p.ReadString(token.LF) // gets rid of the LF!

	// read the tagger
	tagger := p.parseWhoWhen(markerTagger)
	p.ConsumeByte(token.LF)

	// read the commit message
	p.ConsumeByte(token.LF)
	msg := p.String()

	if p.Count() != p.hdr.Size() {
		util.PanicErr("payload doesn't match prescibed size")
	}

	return objects.NewTag(p.oid, target, t, p.hdr, name, msg, tagger)
}
Ejemplo n.º 3
0
func (p *revParser) Parse() error {
	e := util.SafeParse(func() {
		if p.rev == "" {
			util.PanicErr("revision spec is empty")
		}

		if p.PeekByte() == ':' {
			util.PanicErr(": syntaxes not supported") // TODO
		}

		start := p.Count()
		// read until modifier or end
		for !p.EOF() {
			if !isModifier(p.PeekByte()) {
				p.ReadByte()
			} else {
				break
			}
		}
		end := p.Count()

		rev := p.rev[start:end]
		if rev == "" {
			util.PanicErr("revision is empty")
		}

		err := p.findObject(rev)
		if err != nil {
			util.PanicErr(err.Error())
		}

		for !p.EOF() {
			var err error
			b := p.ReadByte()
			if b == '^' {
				if !p.EOF() && p.PeekByte() == '{' {
					p.ConsumeByte('{')
					otype := objects.ObjectType(p.ConsumeStrings(token.ObjectTypes))
					err = applyDereference(p, otype)
					if err != nil {

					}
					p.ConsumeByte('}')
				} else {
					err = applyParentFunc(p, CommitNthParent)
				}
			} else if b == '~' {
				err = applyParentFunc(p, CommitNthAncestor)
			} else {
				util.PanicErrf("unexpected modifier: '%s'", string(b))
			}

			if err != nil {
				util.PanicErr(err.Error())
			}
		}
	})
	return e
}