Skip to content

Commit a9db5cf

Browse files
committed
[remote] implement several functions
1 parent ea15c7b commit a9db5cf

File tree

4 files changed

+130
-63
lines changed

4 files changed

+130
-63
lines changed

graph.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "php_git2.h"
22
#include "php_git2_priv.h"
33
#include "graph.h"
4+
45
/* {{{ proto long git_graph_ahead_behind(resource $repo, string $local, string $upstream)
56
*/
67
PHP_FUNCTION(git_graph_ahead_behind)

refspec.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ PHP_FUNCTION(git_refspec_force)
7878
}
7979
/* }}} */
8080

81-
/* {{{ proto resource git_refspec_direction(resource $spec)
81+
/* {{{ proto long git_refspec_direction(resource $spec)
8282
*/
8383
PHP_FUNCTION(git_refspec_direction)
8484
{
@@ -93,7 +93,7 @@ PHP_FUNCTION(git_refspec_direction)
9393

9494
ZEND_FETCH_RESOURCE(_spec, php_git2_t*, &spec, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
9595
result = git_refspec_direction(PHP_GIT2_V(_spec, refspec));
96-
/* TODO(chobie): implement this */
96+
RETURN_LONG(result);
9797
}
9898
/* }}} */
9999

@@ -137,7 +137,7 @@ PHP_FUNCTION(git_refspec_dst_matches)
137137
}
138138
/* }}} */
139139

140-
/* {{{ proto resource git_refspec_transform(long $outlen, resource $spec, string $name)
140+
/* {{{ proto string git_refspec_transform(long $outlen, resource $spec, string $name)
141141
*/
142142
PHP_FUNCTION(git_refspec_transform)
143143
{
@@ -161,7 +161,7 @@ PHP_FUNCTION(git_refspec_transform)
161161
}
162162
/* }}} */
163163

164-
/* {{{ proto resource git_refspec_rtransform(long $outlen, resource $spec, string $name)
164+
/* {{{ proto string git_refspec_rtransform(long $outlen, resource $spec, string $name)
165165
*/
166166
PHP_FUNCTION(git_refspec_rtransform)
167167
{

remote.c

+122-55
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22
#include "php_git2_priv.h"
33
#include "remote.h"
44

5+
static void php_git2_git_transfer_progress_to_array(git_transfer_progress *progress, zval **out TSRMLS_DC)
6+
{
7+
zval *result;
8+
9+
MAKE_STD_ZVAL(result);
10+
array_init(result);
11+
12+
add_assoc_long_ex(result, ZEND_STRS("total_objects"), progress->total_objects);
13+
add_assoc_long_ex(result, ZEND_STRS("indexed_objects"), progress->indexed_objects);
14+
add_assoc_long_ex(result, ZEND_STRS("received_objects"), progress->received_objects);
15+
add_assoc_long_ex(result, ZEND_STRS("local_objects"), progress->local_objects);
16+
add_assoc_long_ex(result, ZEND_STRS("total_deltas"), progress->total_deltas);
17+
add_assoc_long_ex(result, ZEND_STRS("indexed_deltas"), progress->indexed_deltas);
18+
add_assoc_long_ex(result, ZEND_STRS("received_bytes"), progress->received_bytes);
19+
20+
*out = result;
21+
}
22+
23+
524
/* {{{ proto resource git_remote_create(resource $repo, string $name, string $url)
625
*/
726
PHP_FUNCTION(git_remote_create)
@@ -312,19 +331,21 @@ PHP_FUNCTION(git_remote_get_fetch_refspecs)
312331
PHP_FUNCTION(git_remote_set_fetch_refspecs)
313332
{
314333
int result = 0;
315-
zval *remote = NULL;
334+
zval *remote = NULL, *array = NULL;
316335
php_git2_t *_remote = NULL;
317-
zval *array = NULL;
318336
int error = 0;
337+
git_strarray out = {0};
338+
339+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
340+
"ra", &remote, &array) == FAILURE) {
341+
return;
342+
}
319343

320-
/* TODO(chobie): implement this */
321-
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
322-
// "r<git_strarray>", &remote, &array) == FAILURE) {
323-
// return;
324-
// }
344+
php_git2_array_to_strarray(&out, array TSRMLS_CC);
325345

326346
ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
327-
result = git_remote_set_fetch_refspecs(PHP_GIT2_V(_remote, remote), array);
347+
result = git_remote_set_fetch_refspecs(PHP_GIT2_V(_remote, remote), &out);
348+
php_git2_strarray_free(&out);
328349
RETURN_LONG(result);
329350
}
330351
/* }}} */
@@ -348,7 +369,7 @@ PHP_FUNCTION(git_remote_add_push)
348369

349370
ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
350371
result = git_remote_add_push(PHP_GIT2_V(_remote, remote), refspec);
351-
RETURN_BOOL(result);
372+
RETURN_LONG(result);
352373
}
353374
/* }}} */
354375

@@ -357,22 +378,22 @@ PHP_FUNCTION(git_remote_add_push)
357378
PHP_FUNCTION(git_remote_get_push_refspecs)
358379
{
359380
zval *result;
360-
git_strarray array = {0};
361-
zval *remote = NULL;
381+
git_strarray _array = {0};
382+
zval *remote = NULL, *array = NULL;
362383
php_git2_t *_remote = NULL;
363384
int error = 0;
364385

365386
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
366-
"<git_strarray>r", &array, &remote) == FAILURE) {
387+
"ar", &array, &remote) == FAILURE) {
367388
return;
368389
}
369390

391+
php_git2_strarray_to_array(&_array, array TSRMLS_CC);
370392
ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
371-
error = git_remote_get_push_refspecs(&array, PHP_GIT2_V(_remote, remote));
372-
php_git2_strarray_to_array(&array, &result TSRMLS_CC);
373-
git_strarray_free(&array);
393+
error = git_remote_get_push_refspecs(&_array, PHP_GIT2_V(_remote, remote));
394+
git_strarray_free(&_array);
374395

375-
RETURN_ZVAL(result, 0, 1);
396+
RETURN_LONG(error);
376397
}
377398
/* }}} */
378399

@@ -382,19 +403,20 @@ PHP_FUNCTION(git_remote_get_push_refspecs)
382403
PHP_FUNCTION(git_remote_set_push_refspecs)
383404
{
384405
int result = 0;
385-
zval *remote = NULL;
406+
zval *remote = NULL, *array = NULL;
386407
php_git2_t *_remote = NULL;
387-
zval *array = NULL;
408+
git_strarray _array = {0};
388409
int error = 0;
389410

390-
/* TODO(chobie): implement this */
391-
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
392-
// "r<git_strarray>", &remote, &array) == FAILURE) {
393-
// return;
394-
// }
411+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
412+
"r", &remote, &array) == FAILURE) {
413+
return;
414+
}
395415

416+
php_git2_strarray_to_array(&_array, array TSRMLS_CC);
396417
ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
397-
result = git_remote_set_push_refspecs(PHP_GIT2_V(_remote, remote), array);
418+
result = git_remote_set_push_refspecs(PHP_GIT2_V(_remote, remote), &_array);
419+
git_strarray_free(&_array);
398420
RETURN_LONG(result);
399421
}
400422
/* }}} */
@@ -443,7 +465,7 @@ PHP_FUNCTION(git_remote_get_refspec)
443465
{
444466
const git_refspec *result = NULL;
445467
zval *remote = NULL;
446-
php_git2_t *_remote = NULL;
468+
php_git2_t *_remote = NULL, *out;
447469
long n = 0;
448470

449471
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
@@ -453,11 +475,13 @@ PHP_FUNCTION(git_remote_get_refspec)
453475

454476
ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
455477
result = git_remote_get_refspec(PHP_GIT2_V(_remote, remote), n);
456-
/* TODO(chobie): implement this */
478+
if (php_git2_make_resource(&out, PHP_GIT2_TYPE_REFSPEC, result, 0 TSRMLS_CC)) {
479+
RETURN_FALSE;
480+
}
481+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(out));
457482
}
458483
/* }}} */
459484

460-
461485
/* {{{ proto bool git_remote_connect(resource $remote, long $direction)
462486
*/
463487
PHP_FUNCTION(git_remote_connect)
@@ -479,28 +503,59 @@ PHP_FUNCTION(git_remote_connect)
479503
}
480504
/* }}} */
481505

482-
/* {{{ proto resource git_remote_ls(long $size, resource $remote)
506+
static void php_git2_git_remote_head_to_array(git_remote_head *head, zval **out TSRMLS_DC)
507+
{
508+
zval *result = NULL;
509+
char oid[41] = {0}, loid[41] = {0};
510+
511+
512+
git_oid_fmt(oid, &head->oid);
513+
git_oid_fmt(loid, &head->loid);
514+
515+
MAKE_STD_ZVAL(result);
516+
array_init(result);
517+
add_assoc_long_ex(result, ZEND_STRS("local"), head->local);
518+
add_assoc_string_ex(result, ZEND_STRS("oid"), oid, 1);
519+
add_assoc_string_ex(result, ZEND_STRS("loid"), loid, 1);
520+
add_assoc_string_ex(result, ZEND_STRS("name"), head->name, 1);
521+
522+
*out = result;
523+
}
524+
525+
/* {{{ proto resource git_remote_ls(resource $remote)
483526
*/
484527
PHP_FUNCTION(git_remote_ls)
485528
{
486529
php_git2_t *result = NULL;
487530
git_remote_head **out = NULL;
488-
long size = 0;
489-
zval *remote = NULL;
531+
size_t size = 0;
532+
zval *remote = NULL, *retval = NULL, *container = NULL;
490533
php_git2_t *_remote = NULL;
491-
int error = 0;
534+
int error = 0, i = 0;
492535

493536
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
494-
"lr", &size, &remote) == FAILURE) {
537+
"r", &remote) == FAILURE) {
495538
return;
496539
}
497540

498541
ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
499-
error = git_remote_ls(&out, size, PHP_GIT2_V(_remote, remote));
542+
543+
if (git_remote_connected(PHP_GIT2_V(_remote, remote)) == 0) {
544+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "passed git_remote hasn't been connected");
545+
RETURN_FALSE;
546+
}
547+
error = git_remote_ls(&out, &size, PHP_GIT2_V(_remote, remote));
500548
if (php_git2_check_error(error, "git_remote_ls" TSRMLS_CC)) {
501549
RETURN_FALSE;
502550
}
503-
/* TODO(chobie): implement this */
551+
552+
MAKE_STD_ZVAL(container);
553+
array_init(container);
554+
for (i = 0; i < size; i++) {
555+
php_git2_git_remote_head_to_array(out[i], &retval TSRMLS_CC);
556+
add_next_index_zval(container, retval);
557+
}
558+
RETURN_ZVAL(container, 0, 1);
504559
}
505560
/* }}} */
506561

@@ -851,7 +906,7 @@ PHP_FUNCTION(git_remote_set_callbacks)
851906
PHP_FUNCTION(git_remote_stats)
852907
{
853908
const git_transfer_progress *result = NULL;
854-
zval *remote = NULL;
909+
zval *remote = NULL, *retval = NULL;
855910
php_git2_t *_remote = NULL;
856911

857912
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
@@ -861,7 +916,8 @@ PHP_FUNCTION(git_remote_stats)
861916

862917
ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
863918
result = git_remote_stats(PHP_GIT2_V(_remote, remote));
864-
/* TODO(chobie): implement this */
919+
php_git2_git_transfer_progress_to_array(result, &retval TSRMLS_CC);
920+
RETURN_ZVAL(retval, 0, 1);
865921
}
866922
/* }}} */
867923

@@ -881,7 +937,7 @@ PHP_FUNCTION(git_remote_autotag)
881937

882938
ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
883939
result = git_remote_autotag(PHP_GIT2_V(_remote, remote));
884-
/* TODO(chobie): implement this */
940+
RETURN_LONG(result);
885941
}
886942
/* }}} */
887943

@@ -892,9 +948,8 @@ PHP_FUNCTION(git_remote_set_autotag)
892948
zval *remote = NULL, *value = NULL;
893949
php_git2_t *_remote = NULL;
894950

895-
/* TODO(chobie):impelement this */
896951
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
897-
"r<git_remote_autotag_option_t>", &remote, &value) == FAILURE) {
952+
"rl", &remote, &value) == FAILURE) {
898953
return;
899954
}
900955

@@ -904,27 +959,39 @@ PHP_FUNCTION(git_remote_set_autotag)
904959
/* }}} */
905960

906961

907-
/* {{{ proto long git_remote_rename(remote, new_name, callback, payload)
908-
*/
962+
static int php_git2_git_remote_rename_problem_cb(const char *problematic_refspec, void *payload)
963+
{
964+
return 0;
965+
}
966+
967+
968+
/* {{{ proto long git_remote_rename(resource $remote, string $new_name, Callable $callback, $payload)
969+
*/
909970
PHP_FUNCTION(git_remote_rename)
910971
{
911-
zval *remote;
912-
php_git2_t *_remote;
913-
char *new_name = {0};
914-
int new_name_len;
915-
zval *callback;
916-
php_git2_t *_callback;
972+
int result = 0, new_name_len = 0, error = 0;
973+
zval *remote = NULL, *callback = NULL, *payload = NULL;
974+
php_git2_t *_remote = NULL;
975+
char *new_name = NULL;
976+
zend_fcall_info fci = empty_fcall_info;
977+
zend_fcall_info_cache fcc = empty_fcall_info_cache;
978+
php_git2_cb_t *cb = NULL;
917979

918-
/* TODO(chobie): implement this */
919-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_remote_rename not implemented yet");
920-
return;
980+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
981+
"rsfz", &remote, &new_name, &new_name_len, &fci, &fcc, &payload) == FAILURE) {
982+
return;
983+
}
921984

922-
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
923-
// "rsr", &remote, &new_name, &new_name_len, &callback, &payload) == FAILURE) {
924-
// return;
925-
// }
926-
// ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
985+
ZEND_FETCH_RESOURCE(_remote, php_git2_t*, &remote, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
986+
if (php_git2_cb_init(&cb, &fci, &fcc, payload TSRMLS_CC)) {
987+
RETURN_FALSE;
988+
}
989+
result = git_remote_rename(PHP_GIT2_V(_remote, remote), new_name, php_git2_git_remote_rename_problem_cb, cb);
990+
php_git2_cb_free(cb);
991+
RETURN_LONG(result);
927992
}
993+
/* }}} */
994+
928995

929996
/* {{{ proto long git_remote_update_fetchhead(resource $remote)
930997
*/

remote.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_git_remote_connect, 0, 0, 2)
128128
ZEND_ARG_INFO(0, direction)
129129
ZEND_END_ARG_INFO()
130130

131-
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_remote_ls, 0, 0, 2)
132-
ZEND_ARG_INFO(0, size)
131+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_remote_ls, 0, 0, 1)
133132
ZEND_ARG_INFO(0, remote)
134133
ZEND_END_ARG_INFO()
135134

@@ -205,7 +204,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_git_remote_rename, 0, 0, 4)
205204
ZEND_ARG_INFO(0, remote)
206205
ZEND_ARG_INFO(0, new_name)
207206
ZEND_ARG_INFO(0, callback)
208-
ZEND_ARG_INFO(0, payload)
207+
ZEND_ARG_INFO(1, payload)
209208
ZEND_END_ARG_INFO()
210209

211210
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_remote_update_fetchhead, 0, 0, 1)
@@ -305,7 +304,7 @@ PHP_FUNCTION(git_remote_get_refspec);
305304
*/
306305
PHP_FUNCTION(git_remote_connect);
307306

308-
/* {{{ proto resource git_remote_ls(size, remote)
307+
/* {{{ proto resource git_remote_ls(remote)
309308
*/
310309
PHP_FUNCTION(git_remote_ls);
311310

0 commit comments

Comments
 (0)