1
1
#include "clar_libgit2.h"
2
+ #include "git2/sys/commit.h"
2
3
3
4
static const char * committer_name = "Vicent Marti" ;
4
5
static const char * committer_email = "[email protected] " ;
5
6
static const char * commit_message = "This commit has been created in memory\n\
6
7
This is a commit created in memory and it will be written back to disk\n" ;
7
- static const char * tree_oid = "1810dff58d8a660512d4832e740f692884338ccd" ;
8
+ static const char * tree_id_str = "1810dff58d8a660512d4832e740f692884338ccd" ;
9
+ static const char * parent_id_str = "8496071c1b46c854b31185ea97743be6a8774479" ;
8
10
static const char * root_commit_message = "This is a root commit\n\
9
11
This is a root commit and should be the only one in this branch\n" ;
10
12
static const char * root_reflog_message = "commit (initial): This is a root commit \
@@ -35,6 +37,8 @@ void test_commit_write__cleanup(void)
35
37
head_old = NULL ;
36
38
37
39
cl_git_sandbox_cleanup ();
40
+
41
+ cl_git_pass (git_libgit2_opts (GIT_OPT_ENABLE_STRICT_OBJECT_CREATION , 0 ));
38
42
}
39
43
40
44
@@ -46,12 +50,11 @@ void test_commit_write__from_memory(void)
46
50
const git_signature * author1 , * committer1 ;
47
51
git_commit * parent ;
48
52
git_tree * tree ;
49
- const char * commit_id_str = "8496071c1b46c854b31185ea97743be6a8774479" ;
50
53
51
- git_oid_fromstr (& tree_id , tree_oid );
54
+ git_oid_fromstr (& tree_id , tree_id_str );
52
55
cl_git_pass (git_tree_lookup (& tree , g_repo , & tree_id ));
53
56
54
- git_oid_fromstr (& parent_id , commit_id_str );
57
+ git_oid_fromstr (& parent_id , parent_id_str );
55
58
cl_git_pass (git_commit_lookup (& parent , g_repo , & parent_id ));
56
59
57
60
/* create signatures */
@@ -106,7 +109,7 @@ void test_commit_write__root(void)
106
109
git_reflog * log ;
107
110
const git_reflog_entry * entry ;
108
111
109
- git_oid_fromstr (& tree_id , tree_oid );
112
+ git_oid_fromstr (& tree_id , tree_id_str );
110
113
cl_git_pass (git_tree_lookup (& tree , g_repo , & tree_id ));
111
114
112
115
/* create signatures */
@@ -158,3 +161,101 @@ void test_commit_write__root(void)
158
161
git_signature_free (committer );
159
162
git_reflog_free (log );
160
163
}
164
+
165
+ static int create_commit_from_ids (
166
+ git_oid * result ,
167
+ const git_oid * tree_id ,
168
+ const git_oid * parent_id )
169
+ {
170
+ git_signature * author , * committer ;
171
+ const git_oid * parent_ids [1 ];
172
+ int ret ;
173
+
174
+ cl_git_pass (git_signature_new (
175
+ & committer , committer_name , committer_email , 123456789 , 60 ));
176
+ cl_git_pass (git_signature_new (
177
+ & author , committer_name , committer_email , 987654321 , 90 ));
178
+
179
+ parent_ids [0 ] = parent_id ;
180
+
181
+ ret = git_commit_create_from_ids (
182
+ result ,
183
+ g_repo ,
184
+ NULL ,
185
+ author ,
186
+ committer ,
187
+ NULL ,
188
+ root_commit_message ,
189
+ tree_id ,
190
+ 1 ,
191
+ parent_ids );
192
+
193
+ git_signature_free (committer );
194
+ git_signature_free (author );
195
+
196
+ return ret ;
197
+ }
198
+
199
+ void test_commit_write__doesnt_validate_objects_by_default (void )
200
+ {
201
+ git_oid expected_id , tree_id , parent_id , commit_id ;
202
+
203
+ /* this is a valid tree and parent */
204
+ git_oid_fromstr (& tree_id , tree_id_str );
205
+ git_oid_fromstr (& parent_id , parent_id_str );
206
+
207
+ git_oid_fromstr (& expected_id , "c8571bbec3a72c4bcad31648902e5a453f1adece" );
208
+ cl_git_pass (create_commit_from_ids (& commit_id , & tree_id , & parent_id ));
209
+ cl_assert_equal_oid (& expected_id , & commit_id );
210
+
211
+ /* this is a wholly invented tree id */
212
+ git_oid_fromstr (& tree_id , "1234567890123456789012345678901234567890" );
213
+ git_oid_fromstr (& parent_id , parent_id_str );
214
+
215
+ git_oid_fromstr (& expected_id , "996008340b8e68d69bf3c28d7c57fb7ec3c8e202" );
216
+ cl_git_pass (create_commit_from_ids (& commit_id , & tree_id , & parent_id ));
217
+ cl_assert_equal_oid (& expected_id , & commit_id );
218
+
219
+ /* this is a wholly invented parent id */
220
+ git_oid_fromstr (& tree_id , tree_id_str );
221
+ git_oid_fromstr (& parent_id , "1234567890123456789012345678901234567890" );
222
+
223
+ git_oid_fromstr (& expected_id , "d78f660cab89d9791ca6714b57978bf2a7e709fd" );
224
+ cl_git_pass (create_commit_from_ids (& commit_id , & tree_id , & parent_id ));
225
+ cl_assert_equal_oid (& expected_id , & commit_id );
226
+
227
+ /* these are legitimate objects, but of the wrong type */
228
+ git_oid_fromstr (& tree_id , parent_id_str );
229
+ git_oid_fromstr (& parent_id , tree_id_str );
230
+
231
+ git_oid_fromstr (& expected_id , "5d80c07414e3f18792949699dfcacadf7748f361" );
232
+ cl_git_pass (create_commit_from_ids (& commit_id , & tree_id , & parent_id ));
233
+ cl_assert_equal_oid (& expected_id , & commit_id );
234
+ }
235
+
236
+ void test_commit_write__can_validate_objects (void )
237
+ {
238
+ git_oid tree_id , parent_id , commit_id ;
239
+
240
+ cl_git_pass (git_libgit2_opts (GIT_OPT_ENABLE_STRICT_OBJECT_CREATION , 1 ));
241
+
242
+ /* this is a valid tree and parent */
243
+ git_oid_fromstr (& tree_id , tree_id_str );
244
+ git_oid_fromstr (& parent_id , parent_id_str );
245
+ cl_git_pass (create_commit_from_ids (& commit_id , & tree_id , & parent_id ));
246
+
247
+ /* this is a wholly invented tree id */
248
+ git_oid_fromstr (& tree_id , "1234567890123456789012345678901234567890" );
249
+ git_oid_fromstr (& parent_id , parent_id_str );
250
+ cl_git_fail (create_commit_from_ids (& commit_id , & tree_id , & parent_id ));
251
+
252
+ /* this is a wholly invented parent id */
253
+ git_oid_fromstr (& tree_id , tree_id_str );
254
+ git_oid_fromstr (& parent_id , "1234567890123456789012345678901234567890" );
255
+ cl_git_fail (create_commit_from_ids (& commit_id , & tree_id , & parent_id ));
256
+
257
+ /* these are legitimate objects, but of the wrong type */
258
+ git_oid_fromstr (& tree_id , parent_id_str );
259
+ git_oid_fromstr (& parent_id , tree_id_str );
260
+ cl_git_fail (create_commit_from_ids (& commit_id , & tree_id , & parent_id ));
261
+ }
0 commit comments