Skip to content

Commit f77bba3

Browse files
committed
[packbuilder] add callbacks
1 parent 7a66238 commit f77bba3

File tree

7 files changed

+156
-29
lines changed

7 files changed

+156
-29
lines changed

example/packbuilder.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$repo = git_repository_open(".");
4+
$pb = git_packbuilder_new($repo);
5+
6+
7+
git_packbuilder_insert_commit($pb, "7a66238ef5f12f754b511f0dfec891c061dbc2e0");
8+
git_packbuilder_insert_commit($pb, "c3b172cda6993b06e8ae6b1bc7f1364968e112c9");
9+
10+
$p = array();
11+
git_packbuilder_set_callbacks($pb, function($stage, $current, $total, &$payload){
12+
echo $stage;
13+
}, $p);
14+
git_packbuilder_write($pb, "/tmp/pack", 0,function($stats, &$payload){
15+
var_dump($stats);
16+
}, $p);
17+
git_packbuilder_foreach($pb, function($b, $size, &$payload) {
18+
echo $size . PHP_EOL;
19+
}, $p);
20+
$hash = git_packbuilder_hash($pb);
21+
var_dump($hash);

example/patch.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
$repo = git_repository_open(".");
3+
$tree = git_tree_lookup($repo, "e14ccb8e18d632d78ce2f0aeb06597a03f42b237");
4+
$diff = git_diff_tree_to_workdir($repo, $tree, git_diff_options_init());
5+
6+
$payload = array();
7+
$patch = git_patch_from_diff($diff, 1);
8+
git_patch_print($patch, function($diff_delta, $diff_hunk, $diff_line, $payload){
9+
if ($diff_line['origin'] == "-" || $diff_line['origin'] == "+") {
10+
echo $diff_line['origin'];
11+
}
12+
echo $diff_line['content'];
13+
}, $payload);

helper.c

+17
Original file line numberDiff line numberDiff line change
@@ -738,3 +738,20 @@ int php_git2_git_diff_line_cb(
738738
return retval;
739739
}
740740

741+
void php_git2_git_transfer_progress_to_array(git_transfer_progress *progress, zval **out TSRMLS_DC)
742+
{
743+
zval *result;
744+
745+
MAKE_STD_ZVAL(result);
746+
array_init(result);
747+
748+
add_assoc_long_ex(result, ZEND_STRS("total_objects"), progress->total_objects);
749+
add_assoc_long_ex(result, ZEND_STRS("indexed_objects"), progress->indexed_objects);
750+
add_assoc_long_ex(result, ZEND_STRS("received_objects"), progress->received_objects);
751+
add_assoc_long_ex(result, ZEND_STRS("local_objects"), progress->local_objects);
752+
add_assoc_long_ex(result, ZEND_STRS("total_deltas"), progress->total_deltas);
753+
add_assoc_long_ex(result, ZEND_STRS("indexed_deltas"), progress->indexed_deltas);
754+
add_assoc_long_ex(result, ZEND_STRS("received_bytes"), progress->received_bytes);
755+
756+
*out = result;
757+
}

helper.h

+2
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,6 @@ int php_git2_git_diff_line_cb(
9292
const git_diff_line *line,
9393
void *payload);
9494

95+
void php_git2_git_transfer_progress_to_array(git_transfer_progress *progress, zval **out TSRMLS_DC);
96+
9597
#endif

packbuilder.c

+99-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,92 @@
22
#include "php_git2_priv.h"
33
#include "packbuilder.h"
44

5+
static int php_git2_git_packbuilder_progress(
6+
int stage,
7+
unsigned int current,
8+
unsigned int total,
9+
void *payload)
10+
{
11+
php_git2_t *result;
12+
zval *param_stage = NULL, *param_current = NULL, *param_total = NULL, *retval_ptr = NULL;
13+
php_git2_cb_t *p = (php_git2_cb_t*)payload;
14+
int i = 0;
15+
long retval = 0;
16+
GIT2_TSRMLS_SET(p->tsrm_ls)
17+
18+
Z_ADDREF_P(p->payload);
19+
MAKE_STD_ZVAL(param_stage);
20+
MAKE_STD_ZVAL(param_current);
21+
MAKE_STD_ZVAL(param_total);
22+
ZVAL_LONG(param_stage, stage);
23+
ZVAL_LONG(param_current, current);
24+
ZVAL_LONG(param_total, total);
25+
26+
if (php_git2_call_function_v(p->fci, p->fcc TSRMLS_CC, &retval_ptr, 4,
27+
&param_stage, &param_current, &param_total, &p->payload)) {
28+
return GIT_EUSER;
29+
}
30+
31+
if (retval_ptr) {
32+
retval = Z_LVAL_P(retval_ptr);
33+
zval_ptr_dtor(&retval_ptr);
34+
}
35+
return retval;
36+
}
37+
38+
static int php_git2_git_packbuilder_foreach_cb(void *buf, size_t size, void *payload)
39+
{
40+
php_git2_t *result;
41+
zval *param_buf= NULL, *param_size = NULL, *retval_ptr = NULL;
42+
php_git2_cb_t *p = (php_git2_cb_t*)payload;
43+
int i = 0;
44+
long retval = 0;
45+
GIT2_TSRMLS_SET(p->tsrm_ls)
46+
47+
Z_ADDREF_P(p->payload);
48+
MAKE_STD_ZVAL(param_buf);
49+
MAKE_STD_ZVAL(param_size);
50+
ZVAL_STRINGL(param_buf, buf, size, 1);
51+
ZVAL_LONG(param_size, size);
52+
53+
if (php_git2_call_function_v(p->fci, p->fcc TSRMLS_CC, &retval_ptr, 3,
54+
&param_buf, &param_size, &p->payload)) {
55+
return GIT_EUSER;
56+
}
57+
58+
if (retval_ptr) {
59+
retval = Z_LVAL_P(retval_ptr);
60+
zval_ptr_dtor(&retval_ptr);
61+
}
62+
return retval;
63+
}
64+
65+
66+
static int php_git2_git_transfer_progress_callback(const git_transfer_progress *stats, void *payload)
67+
{
68+
php_git2_t *result;
69+
zval *param_stats = NULL, *retval_ptr = NULL;
70+
php_git2_cb_t *p = (php_git2_cb_t*)payload;
71+
int i = 0;
72+
long retval = 0;
73+
GIT2_TSRMLS_SET(p->tsrm_ls)
74+
75+
Z_ADDREF_P(p->payload);
76+
php_git2_git_transfer_progress_to_array(stats, &param_stats TSRMLS_CC);
77+
78+
79+
if (php_git2_call_function_v(p->fci, p->fcc TSRMLS_CC, &retval_ptr, 2,
80+
&param_stats, &p->payload)) {
81+
return GIT_EUSER;
82+
}
83+
84+
if (retval_ptr) {
85+
retval = Z_LVAL_P(retval_ptr);
86+
zval_ptr_dtor(&retval_ptr);
87+
}
88+
return retval;
89+
}
90+
591
/* {{{ proto resource git_packbuilder_new(resource $repo)
692
*/
793
PHP_FUNCTION(git_packbuilder_new)
@@ -142,7 +228,7 @@ PHP_FUNCTION(git_packbuilder_write)
142228
if (php_git2_cb_init(&cb, &fci, &fcc, progress_cb_payload TSRMLS_CC)) {
143229
RETURN_FALSE;
144230
}
145-
//result = git_packbuilder_write(PHP_GIT2_V(_pb, packbuilder), path, mode, <CHANGEME>, cb);
231+
result = git_packbuilder_write(PHP_GIT2_V(_pb, packbuilder), path, mode, php_git2_git_transfer_progress_callback, cb);
146232
php_git2_cb_free(cb);
147233
RETURN_LONG(result);
148234
}
@@ -169,6 +255,7 @@ PHP_FUNCTION(git_packbuilder_hash)
169255
}
170256
/* }}} */
171257

258+
172259
/* {{{ proto long git_packbuilder_foreach(resource $pb, Callable $cb, $payload)
173260
*/
174261
PHP_FUNCTION(git_packbuilder_foreach)
@@ -189,7 +276,7 @@ PHP_FUNCTION(git_packbuilder_foreach)
189276
if (php_git2_cb_init(&cb, &fci, &fcc, payload TSRMLS_CC)) {
190277
RETURN_FALSE;
191278
}
192-
//result = git_packbuilder_foreach(PHP_GIT2_V(_pb, packbuilder), <CHANGEME>, cb);
279+
result = git_packbuilder_foreach(PHP_GIT2_V(_pb, packbuilder), php_git2_git_packbuilder_foreach_cb, cb);
193280
php_git2_cb_free(cb);
194281
RETURN_LONG(result);
195282
}
@@ -240,8 +327,8 @@ PHP_FUNCTION(git_packbuilder_set_callbacks)
240327
int result = 0, error = 0;
241328
zval *pb = NULL, *progress_cb = NULL, *progress_cb_payload = NULL;
242329
php_git2_t *_pb = NULL;
243-
zend_fcall_info fci = empty_fcall_info;
244-
zend_fcall_info_cache fcc = empty_fcall_info_cache;
330+
zend_fcall_info fci = empty_fcall_info, *_fci;
331+
zend_fcall_info_cache fcc = empty_fcall_info_cache, *_fcc;
245332
php_git2_cb_t *cb = NULL;
246333

247334
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
@@ -250,11 +337,16 @@ PHP_FUNCTION(git_packbuilder_set_callbacks)
250337
}
251338

252339
ZEND_FETCH_RESOURCE(_pb, php_git2_t*, &pb, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
253-
if (php_git2_cb_init(&cb, &fci, &fcc, progress_cb_payload TSRMLS_CC)) {
340+
_fci = emalloc(sizeof(zend_fcall_info));
341+
_fcc = emalloc(sizeof(zend_fcall_info_cache));
342+
memcpy(_fci, &fci, sizeof(zend_fcall_info));
343+
memcpy(_fcc, &fcc, sizeof(zend_fcall_info_cache));
344+
345+
/* TODO(chobie): free memory when the resource removed */
346+
if (php_git2_cb_init(&cb, &_fci, &_fcc, progress_cb_payload TSRMLS_CC)) {
254347
RETURN_FALSE;
255348
}
256-
//result = git_packbuilder_set_callbacks(PHP_GIT2_V(_pb, packbuilder), <CHANGEME>, cb);
257-
php_git2_cb_free(cb);
349+
result = git_packbuilder_set_callbacks(PHP_GIT2_V(_pb, packbuilder), php_git2_git_packbuilder_progress, cb);
258350
RETURN_LONG(result);
259351
}
260352
/* }}} */

packbuilder.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_git_packbuilder_insert_commit, 0, 0, 2)
5151
ZEND_ARG_INFO(0, id)
5252
ZEND_END_ARG_INFO()
5353

54-
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_packbuilder_write, 0, 0, 4)
54+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_packbuilder_write, 0, 0, 5)
55+
ZEND_ARG_INFO(0, pb)
5556
ZEND_ARG_INFO(0, path)
5657
ZEND_ARG_INFO(0, mode)
5758
ZEND_ARG_INFO(0, progress_cb)
58-
ZEND_ARG_INFO(0, progress_cb_payload)
59+
ZEND_ARG_INFO(1, progress_cb_payload)
5960
ZEND_END_ARG_INFO()
6061

6162
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_packbuilder_hash, 0, 0, 1)
@@ -79,7 +80,7 @@ ZEND_END_ARG_INFO()
7980
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_packbuilder_set_callbacks, 0, 0, 3)
8081
ZEND_ARG_INFO(0, pb)
8182
ZEND_ARG_INFO(0, progress_cb)
82-
ZEND_ARG_INFO(0, progress_cb_payload)
83+
ZEND_ARG_INFO(1, progress_cb_payload)
8384
ZEND_END_ARG_INFO()
8485

8586
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_packbuilder_free, 0, 0, 1)

remote.c

-19
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,6 @@
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-
245
/* {{{ proto resource git_remote_create(resource $repo, string $name, string $url)
256
*/
267
PHP_FUNCTION(git_remote_create)

0 commit comments

Comments
 (0)