2013-04-16 16:18:35 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2017-04-19 20:36:00 -05:00
|
|
|
"strings"
|
2013-04-16 16:18:35 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestObjectPoymorphism(t *testing.T) {
|
2016-08-27 12:21:05 -05:00
|
|
|
t.Parallel()
|
2013-04-16 16:18:35 -05:00
|
|
|
repo := createTestRepo(t)
|
2015-04-24 05:59:29 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
|
2013-04-16 16:18:35 -05:00
|
|
|
commitId, treeId := seedTestRepo(t, repo)
|
|
|
|
|
2015-08-04 07:47:10 -05:00
|
|
|
var obj *Object
|
2013-04-16 16:18:35 -05:00
|
|
|
|
|
|
|
commit, err := repo.LookupCommit(commitId)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
2015-08-04 07:47:10 -05:00
|
|
|
obj = &commit.Object
|
2013-09-12 03:40:57 -05:00
|
|
|
if obj.Type() != ObjectCommit {
|
2013-04-16 16:18:35 -05:00
|
|
|
t.Fatalf("Wrong object type, expected commit, have %v", obj.Type())
|
|
|
|
}
|
|
|
|
|
2013-09-12 03:40:57 -05:00
|
|
|
commitTree, err := commit.Tree()
|
|
|
|
checkFatal(t, err)
|
|
|
|
commitTree.EntryCount()
|
|
|
|
|
2013-04-16 16:18:35 -05:00
|
|
|
tree, err := repo.LookupTree(treeId)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
2015-08-04 07:47:10 -05:00
|
|
|
obj = &tree.Object
|
2013-09-12 03:40:57 -05:00
|
|
|
if obj.Type() != ObjectTree {
|
2013-04-16 16:18:35 -05:00
|
|
|
t.Fatalf("Wrong object type, expected tree, have %v", obj.Type())
|
|
|
|
}
|
|
|
|
|
2015-08-04 07:47:10 -05:00
|
|
|
tree2, err := obj.AsTree()
|
|
|
|
if err != nil {
|
2013-04-16 16:18:35 -05:00
|
|
|
t.Fatalf("Converting back to *Tree is not ok")
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:21:16 -05:00
|
|
|
entry := tree2.EntryByName("README")
|
|
|
|
if entry == nil {
|
2013-04-16 16:18:35 -05:00
|
|
|
t.Fatalf("Tree did not have expected \"README\" entry")
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:21:16 -05:00
|
|
|
if entry.Filemode != FilemodeBlob {
|
|
|
|
t.Fatal("Wrong filemode for \"README\"")
|
|
|
|
}
|
|
|
|
|
2015-08-04 07:47:10 -05:00
|
|
|
_, err = obj.AsCommit()
|
|
|
|
if err == nil {
|
2013-04-16 16:18:35 -05:00
|
|
|
t.Fatalf("*Tree is somehow the same as *Commit")
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err = repo.Lookup(tree.Id())
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
2015-08-04 07:47:10 -05:00
|
|
|
_, err = obj.AsTree()
|
|
|
|
if err != nil {
|
2013-04-16 16:18:35 -05:00
|
|
|
t.Fatalf("Lookup creates the wrong type")
|
|
|
|
}
|
|
|
|
|
2013-09-12 03:40:57 -05:00
|
|
|
if obj.Type() != ObjectTree {
|
2013-04-16 16:18:35 -05:00
|
|
|
t.Fatalf("Type() doesn't agree with dynamic type")
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err = repo.RevparseSingle("HEAD")
|
|
|
|
checkFatal(t, err)
|
2013-09-12 03:40:57 -05:00
|
|
|
if obj.Type() != ObjectCommit || obj.Id().String() != commit.Id().String() {
|
2013-04-16 16:18:35 -05:00
|
|
|
t.Fatalf("Failed to parse the right revision")
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err = repo.RevparseSingle("HEAD^{tree}")
|
|
|
|
checkFatal(t, err)
|
2013-09-12 03:40:57 -05:00
|
|
|
if obj.Type() != ObjectTree || obj.Id().String() != tree.Id().String() {
|
2013-04-16 16:18:35 -05:00
|
|
|
t.Fatalf("Failed to parse the right revision")
|
|
|
|
}
|
|
|
|
}
|
2014-05-25 02:06:18 -05:00
|
|
|
|
|
|
|
func checkOwner(t *testing.T, repo *Repository, obj Object) {
|
|
|
|
owner := obj.Owner()
|
|
|
|
if owner == nil {
|
|
|
|
t.Fatal("bad owner")
|
|
|
|
}
|
|
|
|
|
|
|
|
if owner.ptr != repo.ptr {
|
|
|
|
t.Fatalf("bad owner, got %v expected %v\n", owner.ptr, repo.ptr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObjectOwner(t *testing.T) {
|
2016-08-27 12:21:05 -05:00
|
|
|
t.Parallel()
|
2014-05-25 02:06:18 -05:00
|
|
|
repo := createTestRepo(t)
|
2015-04-24 05:59:29 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
|
2014-05-25 02:06:18 -05:00
|
|
|
commitId, treeId := seedTestRepo(t, repo)
|
|
|
|
|
|
|
|
commit, err := repo.LookupCommit(commitId)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
tree, err := repo.LookupTree(treeId)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
2015-08-04 07:47:10 -05:00
|
|
|
checkOwner(t, repo, commit.Object)
|
|
|
|
checkOwner(t, repo, tree.Object)
|
2014-05-25 02:06:18 -05:00
|
|
|
}
|
2015-08-01 07:28:20 -05:00
|
|
|
|
2017-04-19 20:36:00 -05:00
|
|
|
func checkShortId(t *testing.T, Id, shortId string) {
|
|
|
|
if len(shortId) < 7 || len(shortId) >= len(Id) {
|
2017-07-11 22:52:13 -05:00
|
|
|
t.Fatalf("bad shortId length %d", len(shortId))
|
2017-04-19 20:36:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(Id, shortId) {
|
|
|
|
t.Fatalf("bad shortId, should be prefix of %s, but is %s\n", Id, shortId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObjectShortId(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
repo := createTestRepo(t)
|
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
|
|
|
|
commitId, _ := seedTestRepo(t, repo)
|
|
|
|
|
|
|
|
commit, err := repo.LookupCommit(commitId)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
shortId, err := commit.ShortId()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
checkShortId(t, commitId.String(), shortId)
|
|
|
|
}
|
|
|
|
|
2015-08-01 07:28:20 -05:00
|
|
|
func TestObjectPeel(t *testing.T) {
|
2016-08-27 12:21:05 -05:00
|
|
|
t.Parallel()
|
2015-08-01 07:28:20 -05:00
|
|
|
repo := createTestRepo(t)
|
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
|
|
|
|
commitID, treeID := seedTestRepo(t, repo)
|
|
|
|
|
2015-08-04 07:47:10 -05:00
|
|
|
var obj *Object
|
2015-08-01 07:28:20 -05:00
|
|
|
|
|
|
|
commit, err := repo.LookupCommit(commitID)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
obj, err = commit.Peel(ObjectAny)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if obj.Type() != ObjectTree {
|
|
|
|
t.Fatalf("Wrong object type when peeling a commit, expected tree, have %v", obj.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err = commit.Peel(ObjectTag)
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
|
|
|
|
t.Fatalf("Wrong error when peeling a commit to a tag, expected ErrorCodeInvalidSpec, have %v", err)
|
2015-08-01 07:28:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tree, err := repo.LookupTree(treeID)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
obj, err = tree.Peel(ObjectAny)
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
|
|
|
|
t.Fatalf("Wrong error when peeling a tree, expected ErrorCodeInvalidSpec, have %v", err)
|
2015-08-01 07:28:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
entry := tree.EntryByName("README")
|
|
|
|
|
|
|
|
blob, err := repo.LookupBlob(entry.Id)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
obj, err = blob.Peel(ObjectAny)
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
|
|
|
|
t.Fatalf("Wrong error when peeling a blob, expected ErrorCodeInvalidSpec, have %v", err)
|
2015-08-01 07:28:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tagID := createTestTag(t, repo, commit)
|
|
|
|
|
|
|
|
tag, err := repo.LookupTag(tagID)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
obj, err = tag.Peel(ObjectAny)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if obj.Type() != ObjectCommit {
|
|
|
|
t.Fatalf("Wrong object type when peeling a tag, expected commit, have %v", obj.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Should test a tag that annotates a different object than a commit
|
|
|
|
// but it's impossible at the moment to tag such an object.
|
|
|
|
}
|