Example #1
0
// Test_readCommits will compare the commit output of
// git and ggit for a string of commits.
func Test_readTree(t *testing.T) {
	testCase := test.TreeDiff
	repo := api.Open(testCase.Repo())
	info, _ := testCase.Info().(*test.InfoTreeDiff)

	oid := objects.OidNow(info.TreeOid1)
	o, err := repo.ObjectFromOid(oid)
	util.AssertNoErr(t, err)
	// get the tree
	tree1, _ := o.(*objects.Tree)

	oid = objects.OidNow(info.TreeOid2)
	o, err = repo.ObjectFromOid(oid)
	util.AssertNoErr(t, err)
	// get the tree
	tree2, _ := o.(*objects.Tree)

	diff, err := NewTreeDiffer(repo, NewBlobComparator()).Diff(tree1, tree2)
	modified := diff.Modified()

	if len(modified) != 1 {
		t.Errorf("expected 1 modification in the tree diff. got %d", len(modified))
	}

	edit := modified[0]
	before, after := edit.Before, edit.After
	beforeOid, afterOid := before.ObjectId(), after.ObjectId()
	beforeName := before.Name()

	if beforeName != info.ModifiedFileName {
		t.Errorf("expected modified file name to be [%s], but got [%s]", info.ModifiedFileName, beforeName)
	}

	if beforeOid.String() != info.ModifiedFileBeforeOid {
		t.Errorf("expected modified file's initial id to be [%s], but got [%s]", info.ModifiedFileBeforeOid, beforeOid)
	}

	if afterOid.String() != info.ModifiedFileAfterOid {
		t.Errorf("expected modified file's initial id to be [%s], but got [%s]", info.ModifiedFileAfterOid, afterOid)
	}

	deleted := diff.Deleted()

	util.Assert(t, len(deleted) == 1, fmt.Sprintf("expected 1 file to be deleted, got [%d]", len(deleted)))
	entry := deleted[0]
	deletedName, deletedOid := entry.Name(), entry.ObjectId().String()
	if deletedName != info.RemovedFileName {
		t.Errorf("expected deleted file's name to be [%s], but got [%s]", info.RemovedFileName, deletedName)
	}
	if deletedOid != info.RemovedFileOid {
		t.Errorf("expected deleted file's id to be [%s], but got [%s]", info.RemovedFileOid, deletedOid)
	}
}
Example #2
0
func Test_revParse__secondAncestor(t *testing.T) {
	testCase := test.Linear
	repo := Open(testCase.Repo())
	info := testCase.Info().(*test.InfoLinear)

	util.Assert(t, info.N > 2)
	util.Assert(t, len(info.Commits) == info.N)

	// test the first, parentless commit
	for i, c := range info.Commits[2:] {
		oid, expOid := objects.OidNow(c.CommitOid), objects.OidNow(info.Commits[i].CommitOid)
		testSecondAncestor(t, repo, oid, expOid)
		testSecondAncestorVariations(t, repo, oid, expOid)
	}
}
Example #3
0
func packedCommitOid() (repo Repository, oid *objects.ObjectId) {
	testRepo := test.LinearPacked
	info := testRepo.Info().(*test.InfoLinearPacked)
	repo = Open(testRepo.Repo())
	oid = objects.OidNow(info.Commits[1].CommitOid)
	return
}
Example #4
0
func packedBlobOid() (repo Repository, oid *objects.ObjectId) {
	testRepo := test.DerefsPacked
	info := testRepo.Info().(*test.InfoDerefsPacked)
	repo = Open(testRepo.Repo())
	oid = objects.OidNow(info.BlobOid)
	return
}
Example #5
0
func Benchmark__readPackedTreeByOid(b *testing.B) {
	b.StopTimer()
	repo, info := packedDerefs()
	oid := objects.OidNow(info.TreeOid)
	for i := 0; i < b.N; i++ {
		objectFromOid(b, repo, oid)
	}
}
Example #6
0
func Test_revParse__derefs(t *testing.T) {
	testCase := test.Derefs
	repo := Open(testCase.Repo())
	info := testCase.Info().(*test.InfoDerefs)

	commitOid := objects.OidNow(info.CommitOid)
	tagOid := objects.OidNow(info.TagOid)
	treeOid := objects.OidNow(info.TreeOid)
	testObjectExpected(t, repo, "HEAD", commitOid, objects.ObjectCommit)
	testObjectExpected(t, repo, "HEAD^{commit}", commitOid, objects.ObjectCommit)
	testObjectExpected(t, repo, "HEAD^{tree}", treeOid, objects.ObjectTree)
	testObjectExpected(t, repo, "HEAD^{commit}^{tree}", treeOid, objects.ObjectTree)
	testObjectExpected(t, repo, info.TagName, tagOid, objects.ObjectTag)
	testObjectExpected(t, repo, info.TagName+"^{commit}", commitOid, objects.ObjectCommit)
	testObjectExpected(t, repo, info.TagName+"^{commit}^{tree}", treeOid, objects.ObjectTree)

}
Example #7
0
func Test_revParse__firstParent(t *testing.T) {
	testCase := test.Linear
	repo := Open(testCase.Repo())
	info := testCase.Info().(*test.InfoLinear)

	util.Assert(t, info.N > 1)
	util.Assert(t, len(info.Commits) == info.N)

	// test the first, parentless commit
	testParentlessCommit(t, repo, objects.OidNow(info.Commits[0].CommitOid))
	for _, c := range info.Commits[1:] {
		oid, expOid := objects.OidNow(c.CommitOid), objects.OidNow(c.ParentOid)
		testShortOid(t, repo, oid)
		testFirstParent(t, repo, oid, expOid)
		testFirstParentVariations(t, repo, oid, expOid)
	}
}
Example #8
0
func Benchmark__derefPackedTreeFromCommit(b *testing.B) {
	b.StopTimer()
	repo, info := packedDerefs()
	oid := objects.OidNow(info.CommitOid)
	rev := oid.String() + "^{tree}"
	for i := 0; i < b.N; i++ {
		objectFromRev(b, repo, rev)
	}
}
Example #9
0
func justTag() (Repository, *objects.Tag) {
	repo, info := packedDerefs()
	tagOid := objects.OidNow(info.TagOid)
	o, err := repo.ObjectFromOid(tagOid)
	if err != nil {
		panic(err)
	}
	return repo, o.(*objects.Tag)
}
Example #10
0
func Benchmark__oidToString(b *testing.B) {
	b.StopTimer()
	const oidStr = "fb5b685ca75023e129b2f3c8172a7a166ad4dca8"
	for i := 0; i < b.N; i++ {
		oid := objects.OidNow(oidStr)
		b.StartTimer()
		oid.String()
		b.StopTimer()
	}
}
Example #11
0
func Test_revParse__zeros(t *testing.T) {
	testCase := test.Linear
	repo := Open(testCase.Repo())
	info := testCase.Info().(*test.InfoLinear)

	util.Assert(t, info.N > 0)
	util.Assert(t, len(info.Commits) == info.N)

	// test the first, parentless commit
	for _, c := range info.Commits {
		oid := objects.OidNow(c.CommitOid)
		testZeros(t, repo, oid)
	}
}
Example #12
0
func Test_makeHash(t *testing.T) {
	testRepo := test.DerefsPacked
	repo := Open(testRepo.Repo())
	info := testRepo.Info().(*test.InfoDerefsPacked)
	tagOid := objects.OidNow(info.TagOid)
	o, err := repo.ObjectFromOid(tagOid)
	if err != nil {
		panic(err)
	}
	if h, err := MakeHash(o); err != nil {
		panic(err)
	} else {
		oid := objects.OidFromHash(h)
		util.Assert(t, oid.String() == tagOid.String())
	}
}
Example #13
0
// Test_readCommits will compare the commit output of
// git and ggit for a string of commits.
func Test_readTree(t *testing.T) {
	testCase := test.Tree
	repo := Open(testCase.Repo())
	info := testCase.Info().(*test.InfoTree)
	f := format.NewStrFormat()

	var (
		oid = objects.OidNow(info.TreeOid)
	)
	o, err := repo.ObjectFromOid(oid)
	util.AssertNoErr(t, err)

	// check the id
	util.Assert(t, o.ObjectId().String() == info.TreeOid)

	// check the header
	util.Assert(t, o.Header().Type() == objects.ObjectTree)
	util.AssertEqualInt(t, int(o.Header().Size()), info.TreeSize)

	// get the tree
	// now convert to a tag and check the fields
	var tree *objects.Tree
	util.AssertPanicFree(t, func() {
		tree = o.(*objects.Tree)
	})

	// check entries
	entries := tree.Entries()
	util.AssertEqualInt(t, info.N, len(entries))
	util.Assert(t, info.N > 2)

	// check a file
	file := entries[0]
	util.AssertEqualString(t, info.File1Oid, file.ObjectId().String())
	util.Assert(t, file.Mode() == objects.ModeBlob)
	// TODO: add checks for type, etc.

	// check the output
	// check the whole representation, which will catch
	// most of the other stuff
	f.Reset()
	f.Object(o)
	util.AssertEqualString(t, info.TreeRepr, f.String())
}
Example #14
0
// this test implements basic blob reading from
// a git repository.
func Test_readBlobs(t *testing.T) {
	testRepo := test.Blobs

	repo := Open(testRepo.Repo())
	info := testRepo.Info().(*test.InfoBlobs)

	if len(info.Blobs) < 1 {
		fmt.Println("warning: no blobs to test")
	}

	for _, detail := range info.Blobs {
		// read the blob
		oid := objects.OidNow(detail.Oid)
		o, err := repo.ObjectFromOid(oid)
		util.AssertNoErr(t, err)
		util.Assert(t, o.Header().Type() == objects.ObjectBlob)
		b := o.(*objects.Blob)
		util.AssertEqualString(t, string(b.Data()), detail.Contents)
		util.AssertEqualInt(t, int(b.Header().Size()), len(detail.Contents))
	}

}
Example #15
0
func Benchmark__oidFromString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		objects.OidNow("fb5b685ca75023e129b2f3c8172a7a166ad4dca8")
	}
}
Example #16
0
func Test_refPaths(t *testing.T) {
	testRepo := test.Refs
	repo := Open(testRepo.Repo())
	info := testRepo.Info().(*test.InfoRefs)

	var (
		oid    = objects.OidNow(info.CommitOid)
		tagOid = objects.OidNow(info.AnnTagOid)

		master   = "refs/heads/master"
		branch   = expandHeadRef(info.BranchName)
		annTag   = expandTagRef(info.AnnTagName)
		lightTag = expandTagRef(info.LightTagName)
	)

	// test reading these full path refs directly from
	// the repository files, loose or packed
	testRefPathPeeled(t, repo, master, oid)
	testRefPathPeeled(t, repo, branch, oid)
	testRefPathPeeled(t, repo, annTag, tagOid)
	testRefPathPeeled(t, repo, lightTag, oid)

	// test reading symbolic refs and asserting that the
	// targets are in fact symbols and are correct
	testRefPathSymbolic(t, repo, info.SymbolicRef1, info.SymbolicRef1Target)
	testRefPathSymbolic(t, repo, info.SymbolicRef2, info.SymbolicRef2Target)
	testRefPathSymbolic(t, repo, "HEAD", master)

	// test that packed refs have correct commit
	// dereferencing information stored in the packed-refs file
	testPackedTagDerefInfo(t, repo, annTag, oid)

	// test ref peeling
	testPeelRef(t, repo, master, oid)
	testPeelRef(t, repo, branch, oid)
	testPeelRef(t, repo, info.SymbolicRef1, oid)
	testPeelRef(t, repo, info.SymbolicRef2, oid)

	// make sure we read loose refs correctly
	testRefRetrieval(t, repo, func() ([]objects.Ref, error) {
		return repo.LooseRefs()
	}, []string{master, branch})

	// make sure we read packed refs correctly
	testRefRetrieval(t, repo, func() ([]objects.Ref, error) {
		return repo.PackedRefs()
	}, []string{annTag, lightTag})

	// make sure we get all refs correctly
	testRefRetrieval(t, repo, func() ([]objects.Ref, error) {
		return repo.Refs()
	}, []string{master, branch, annTag, lightTag})

	// test non existent refs
	testNonexistent(t, repo)

	// test resolution of short refs
	testShortRefResolvesPeeled(t, repo, "master", oid)
	testShortRefResolvesPeeled(t, repo, info.BranchName, oid)
	testShortRefResolvesPeeled(t, repo, info.AnnTagName, tagOid)
	testShortRefResolvesPeeled(t, repo, info.LightTagName, oid)
	testShortRefResolvesSymbolic(t, repo, "HEAD", master, oid)
	testShortRefResolvesSymbolic(t, repo, info.SymbolicRef1, info.SymbolicRef1Target, oid)
	testShortRefResolvesSymbolic(t, repo, info.SymbolicRef2, info.SymbolicRef2Target, oid)

}