@@ -102,3 +102,63 @@ func TestObjectOwner(t *testing.T) {
102
102
checkOwner (t , repo , commit )
103
103
checkOwner (t , repo , tree )
104
104
}
105
+
106
+ func TestObjectPeel (t * testing.T ) {
107
+ repo := createTestRepo (t )
108
+ defer cleanupTestRepo (t , repo )
109
+
110
+ commitID , treeID := seedTestRepo (t , repo )
111
+
112
+ var obj Object
113
+
114
+ commit , err := repo .LookupCommit (commitID )
115
+ checkFatal (t , err )
116
+
117
+ obj , err = commit .Peel (ObjectAny )
118
+ checkFatal (t , err )
119
+
120
+ if obj .Type () != ObjectTree {
121
+ t .Fatalf ("Wrong object type when peeling a commit, expected tree, have %v" , obj .Type ())
122
+ }
123
+
124
+ obj , err = commit .Peel (ObjectTag )
125
+
126
+ if ! IsErrorCode (err , ErrInvalidSpec ) {
127
+ t .Fatalf ("Wrong error when peeling a commit to a tag, expected ErrInvalidSpec, have %v" , err )
128
+ }
129
+
130
+ tree , err := repo .LookupTree (treeID )
131
+ checkFatal (t , err )
132
+
133
+ obj , err = tree .Peel (ObjectAny )
134
+
135
+ if ! IsErrorCode (err , ErrInvalidSpec ) {
136
+ t .Fatalf ("Wrong error when peeling a tree, expected ErrInvalidSpec, have %v" , err )
137
+ }
138
+
139
+ entry := tree .EntryByName ("README" )
140
+
141
+ blob , err := repo .LookupBlob (entry .Id )
142
+ checkFatal (t , err )
143
+
144
+ obj , err = blob .Peel (ObjectAny )
145
+
146
+ if ! IsErrorCode (err , ErrInvalidSpec ) {
147
+ t .Fatalf ("Wrong error when peeling a blob, expected ErrInvalidSpec, have %v" , err )
148
+ }
149
+
150
+ tagID := createTestTag (t , repo , commit )
151
+
152
+ tag , err := repo .LookupTag (tagID )
153
+ checkFatal (t , err )
154
+
155
+ obj , err = tag .Peel (ObjectAny )
156
+ checkFatal (t , err )
157
+
158
+ if obj .Type () != ObjectCommit {
159
+ t .Fatalf ("Wrong object type when peeling a tag, expected commit, have %v" , obj .Type ())
160
+ }
161
+
162
+ // TODO: Should test a tag that annotates a different object than a commit
163
+ // but it's impossible at the moment to tag such an object.
164
+ }
0 commit comments