@@ -44,6 +44,11 @@ zend_object_value php_git2_commit_new(zend_class_entry *ce TSRMLS_DC)
4444 return retval ;
4545}
4646
47+ ZEND_BEGIN_ARG_INFO_EX (arginfo_git2_commit_create , 0 ,0 ,2 )
48+ ZEND_ARG_INFO (0 , repository )
49+ ZEND_ARG_INFO (0 , data )
50+ ZEND_END_ARG_INFO ()
51+
4752/*
4853{{{ proto: Git2\Commit::getMessage()
4954*/
@@ -71,7 +76,7 @@ PHP_METHOD(git2_commit, getMessage)
7176*/
7277PHP_METHOD (git2_commit , getMessageEncoding )
7378{
74- char * encoding ;
79+ const char * encoding ;
7580 php_git2_commit * m_commit ;
7681
7782 m_commit = PHP_GIT2_GET_OBJECT (php_git2_commit , getThis ());
@@ -163,12 +168,143 @@ PHP_METHOD(git2_commit, getCommitter)
163168}
164169/* }}} */
165170
171+ /*
172+ {{{ proto: Git2\Commit::create(Git2\Repository $repo, array $data)
173+ */
174+ PHP_METHOD (git2_commit , create )
175+ {
176+ php_git2_tree * m_tree ;
177+ php_git2_signature * m_author ,* m_committer ;
178+ php_git2_repository * m_repository ;
179+ zval * repository , * * element , * z_parents , * z_tree , * z_author , * z_committer , * z_array , * * value_pp = NULL ;
180+ HashTable * hash ;
181+ const git_commit * * parents = NULL ;
182+ git_commit * * free_list = NULL ;
183+ git_tree * tree ;
184+ git_signature * author , * committer ;
185+ git_oid commit_oid ;
186+ char * message , * encoding , * ref , oid_out [GIT_OID_HEXSZ ];
187+ int parent_count , i , error = 0 ;
188+ HashPosition pos ;
189+
190+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
191+ "Oa" , & repository , git2_repository_class_entry ,& z_array ) == FAILURE ) {
192+ return ;
193+ }
194+
195+ m_repository = PHP_GIT2_GET_OBJECT (php_git2_repository , repository );
196+
197+ hash = Z_ARRVAL_P (z_array );
198+ if (zend_hash_find (hash ,"author" ,sizeof ("author" ),(void * * )& value_pp ) != FAILURE ) {
199+ z_author = * value_pp ;
200+ m_author = PHP_GIT2_GET_OBJECT (php_git2_signature ,z_author );
201+ }
202+
203+ if (zend_hash_find (hash ,"committer" ,sizeof ("committer" ),(void * * )& value_pp ) != FAILURE ) {
204+ z_committer = * value_pp ;
205+ m_committer = PHP_GIT2_GET_OBJECT (php_git2_signature ,z_committer );
206+ } else {
207+ z_committer = z_author ;
208+ m_committer = PHP_GIT2_GET_OBJECT (php_git2_signature ,z_committer );
209+ }
210+
211+ if (zend_hash_find (hash ,"tree" ,sizeof ("tree" ),(void * * )& value_pp ) != FAILURE ) {
212+ z_tree = * value_pp ;
213+ if (Z_TYPE_P (z_tree ) == IS_STRING ) {
214+ git_oid oid ;
215+ error = git_oid_fromstr (& oid , Z_STRVAL_P (z_tree ));
216+ error = git_tree_lookup (& tree , m_repository -> repository ,& oid );
217+ } else {
218+ m_tree = PHP_GIT2_GET_OBJECT (php_git2_tree , z_tree );
219+ tree = m_tree -> tree ;
220+ }
221+ }
222+
223+ if (zend_hash_find (hash ,"parents" ,sizeof ("parents" ),(void * * )& value_pp ) != FAILURE ) {
224+ z_parents = * value_pp ;
225+ }
226+
227+ if (zend_hash_find (hash ,"ref" ,sizeof ("ref" ),(void * * )& value_pp ) != FAILURE ) {
228+ ref = emalloc (sizeof (char )* Z_STRLEN_PP (value_pp )+ 1 );
229+ sprintf (ref , Z_STRVAL_PP (value_pp ));
230+ } else {
231+ ref = emalloc (sizeof (char )* 5 );
232+ sprintf (ref ,"HEAD" );
233+ }
234+
235+ if (zend_hash_find (hash ,"message" ,sizeof ("message" ),(void * * )& value_pp ) != FAILURE ) {
236+ message = Z_STRVAL_PP (value_pp );
237+ }
238+
239+ if (zend_hash_find (hash ,"encoding" ,sizeof ("encoding" ),(void * * )& value_pp ) != FAILURE ) {
240+ encoding = emalloc (sizeof (char )* Z_STRLEN_PP (value_pp )+ 1 );
241+ sprintf (encoding , Z_STRVAL_PP (value_pp ));
242+ } else {
243+ encoding = emalloc (sizeof (char )* 6 );
244+ sprintf (encoding ,"UTF-8" );
245+ }
246+
247+ parent_count = zend_hash_num_elements (Z_ARRVAL_P (z_parents ));
248+ parents = emalloc (parent_count * sizeof (void * ));
249+ free_list = emalloc (parent_count * sizeof (void * ));
250+
251+ for (i = 0 , zend_hash_internal_pointer_reset_ex (Z_ARRVAL_P (z_parents ), & pos );
252+ zend_hash_get_current_data_ex (Z_ARRVAL_P (z_parents ), (void * * )& element , & pos ) == SUCCESS ;
253+ zend_hash_move_forward_ex (Z_ARRVAL_P (z_parents ), & pos )
254+ ) {
255+ git_commit * parent = NULL ;
256+ git_commit * free_ptr = NULL ;
257+
258+ if (Z_TYPE_PP (element ) == IS_STRING ) {
259+ git_oid oid ;
260+ error = git_oid_fromstr (& oid , Z_STRVAL_PP (element ));
261+ git_commit_lookup (& parent , m_repository -> repository ,& oid );
262+
263+ free_ptr = parent ;
264+ } else {
265+ php_git2_commit * m_commit ;
266+ m_commit = PHP_GIT2_GET_OBJECT (php_git2_commit , * element );
267+ parent = m_commit -> commit ;
268+ }
269+
270+ parents [i ] = parent ;
271+ free_list [i ] = free_ptr ;
272+ i ++ ;
273+ }
274+
275+ error = git_commit_create (
276+ & commit_oid ,
277+ m_repository -> repository ,
278+ ref ,
279+ m_author -> signature ,
280+ m_committer -> signature ,
281+ encoding ,
282+ message ,
283+ tree ,
284+ parent_count ,
285+ parents
286+ );
287+
288+ git_oid_fmt (oid_out , & commit_oid );
289+ RETVAL_STRINGL (oid_out ,GIT_OID_HEXSZ ,1 );
290+
291+ for (i = 0 ; i < parent_count ; ++ i ) {
292+ git_object_free ((git_object * )free_list [i ]);
293+ }
294+ efree (ref );
295+ efree (parents );
296+ efree (free_list );
297+ efree (encoding );
298+ }
299+ /* }}} */
300+
166301static zend_function_entry php_git2_commit_methods [] = {
167302 PHP_ME (git2_commit , getMessage , NULL , ZEND_ACC_PUBLIC )
168303 PHP_ME (git2_commit , getMessageEncoding , NULL , ZEND_ACC_PUBLIC )
169304 PHP_ME (git2_commit , parentCount , NULL , ZEND_ACC_PUBLIC )
170305 PHP_ME (git2_commit , getAuthor , NULL , ZEND_ACC_PUBLIC )
171306 PHP_ME (git2_commit , getCommitter , NULL , ZEND_ACC_PUBLIC )
307+ PHP_ME (git2_commit , create , arginfo_git2_commit_create , ZEND_ACC_STATIC | ZEND_ACC_PUBLIC )
172308 {NULL ,NULL ,NULL }
173309};
174310
0 commit comments