Skip to content

Commit e5997aa

Browse files
committed
[repository] improve codes
1 parent bd04439 commit e5997aa

File tree

7 files changed

+385
-237
lines changed

7 files changed

+385
-237
lines changed

helper.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "php_git2.h"
2+
#include "php_git2_priv.h"
23
#include "helper.h"
34

45
static zval* datetime_instantiate(zend_class_entry *pce, zval *object TSRMLS_DC)
@@ -135,3 +136,114 @@ void php_git2_strarray_to_array(git_strarray *array, zval **out TSRMLS_DC)
135136
}
136137
*out = result;
137138
}
139+
140+
int php_git2_make_resource(php_git2_t **out, enum php_git2_resource_type type, void *resource, int should_free TSRMLS_DC)
141+
{
142+
php_git2_t *result = NULL;
143+
144+
PHP_GIT2_MAKE_RESOURCE_NOCHECK(result);
145+
if (result == NULL) {
146+
return 1;
147+
}
148+
149+
switch (type) {
150+
case PHP_GIT2_TYPE_REPOSITORY:
151+
PHP_GIT2_V(result, repository) = (git_repository*)resource;
152+
break;
153+
case PHP_GIT2_TYPE_COMMIT:
154+
PHP_GIT2_V(result, commit) = (git_commit*)resource;
155+
break;
156+
case PHP_GIT2_TYPE_TREE:
157+
PHP_GIT2_V(result, commit) = (git_tree*)resource;
158+
break;
159+
case PHP_GIT2_TYPE_TREE_ENTRY:
160+
PHP_GIT2_V(result, tree_entry) = (git_tree_entry*)resource;
161+
break;
162+
case PHP_GIT2_TYPE_BLOB:
163+
PHP_GIT2_V(result, blob) = (git_blob*)resource;
164+
break;
165+
case PHP_GIT2_TYPE_REVWALK:
166+
PHP_GIT2_V(result, revwalk) = (git_revwalk*)resource;
167+
break;
168+
case PHP_GIT2_TYPE_TREEBUILDER:
169+
PHP_GIT2_V(result, treebuilder) = (git_treebuilder*)resource;
170+
break;
171+
case PHP_GIT2_TYPE_REFERENCE:
172+
PHP_GIT2_V(result, reference) = (git_reference*)resource;
173+
break;
174+
case PHP_GIT2_TYPE_CONFIG:
175+
PHP_GIT2_V(result, config) = (git_config*)resource;
176+
break;
177+
case PHP_GIT2_TYPE_OBJECT:
178+
PHP_GIT2_V(result, object) = (git_object*)resource;
179+
break;
180+
case PHP_GIT2_TYPE_INDEX:
181+
PHP_GIT2_V(result, index) = (git_index*)resource;
182+
break;
183+
case PHP_GIT2_TYPE_ODB:
184+
PHP_GIT2_V(result, odb) = (git_odb*)resource;
185+
break;
186+
case PHP_GIT2_TYPE_REFDB:
187+
PHP_GIT2_V(result, refdb) = (git_refdb*)resource;
188+
break;
189+
case PHP_GIT2_TYPE_STATUS_LIST:
190+
PHP_GIT2_V(result, status_list) = (git_status_list*)resource;
191+
break;
192+
case PHP_GIT2_TYPE_BRANCH_ITERATOR:
193+
PHP_GIT2_V(result, branch_iterator) = (git_branch_iterator*)resource;
194+
break;
195+
case PHP_GIT2_TYPE_TAG:
196+
PHP_GIT2_V(result, tag) = (git_tag*)resource;
197+
break;
198+
case PHP_GIT2_TYPE_CRED:
199+
PHP_GIT2_V(result, cred) = (git_cred*)resource;
200+
break;
201+
case PHP_GIT2_TYPE_TRANSPORT:
202+
PHP_GIT2_V(result, transport) = (git_transport*)resource;
203+
break;
204+
case PHP_GIT2_TYPE_REMOTE:
205+
PHP_GIT2_V(result, remote) = (git_remote*)resource;
206+
break;
207+
case PHP_GIT2_TYPE_DIFF:
208+
PHP_GIT2_V(result, diff) = (git_diff*)resource;
209+
break;
210+
case PHP_GIT2_TYPE_MERGE_RESULT:
211+
PHP_GIT2_V(result, merge_result) = (git_merge_result*)resource;
212+
break;
213+
case PHP_GIT2_TYPE_MERGE_HEAD:
214+
PHP_GIT2_V(result, merge_head) = (git_merge_head*)resource;
215+
break;
216+
case PHP_GIT2_TYPE_PATHSPEC:
217+
PHP_GIT2_V(result, pathspec) = (git_pathspec*)resource;
218+
break;
219+
case PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST:
220+
PHP_GIT2_V(result, pathspec_match_list) = (git_pathspec_match_list*)resource;
221+
break;
222+
case PHP_GIT2_TYPE_PATCH:
223+
PHP_GIT2_V(result, patch) = (git_patch*)resource;
224+
break;
225+
case PHP_GIT2_TYPE_DIFF_HUNK:
226+
PHP_GIT2_V(result, diff_hunk) = (git_diff_hunk*)resource;
227+
break;
228+
case PHP_GIT2_TYPE_BUF:
229+
PHP_GIT2_V(result, buf) = (git_buf*)resource;
230+
break;
231+
case PHP_GIT2_TYPE_FILTER_LIST:
232+
PHP_GIT2_V(result, filter_list) = (git_filter_list*)resource;
233+
break;
234+
case PHP_GIT2_TYPE_FILTER_SOURCE:
235+
PHP_GIT2_V(result, filter_source) = (git_filter_source*)resource;
236+
break;
237+
case PHP_GIT2_TYPE_DIFF_LINE:
238+
PHP_GIT2_V(result, diff_line) = (git_diff_line*)resource;
239+
break;
240+
}
241+
242+
result->type = type;
243+
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
244+
result->should_free_v = should_free;
245+
246+
*out = result;
247+
return 0;
248+
}
249+

helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ void php_git2_signature_to_array(const git_signature *signature, zval **out TSRM
4242

4343
void php_git2_strarray_to_array(git_strarray *array, zval **out TSRMLS_DC);
4444

45+
int php_git2_make_resource(php_git2_t **out, enum php_git2_resource_type type, void *resource, int should_free TSRMLS_DC);
46+
4547
#endif

ng.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,10 @@ public function generateFunctionCall(Printer $printer, Func $f)
778778
} else if ($f->isSavior()) {
779779

780780
$first = $f->first();
781-
$printer->put("if (_`name`->should_free_v) {\n",
781+
$printer->put("if (GIT2_SHOULD_FREE(_`name`)) {\n",
782782
"name", $first->getName()
783783
);
784-
$printer->block(function(Printer $printer) use ($f) {
784+
$printer->block(function(Printer $printer) use ($f, $first) {
785785
$printer->put("`function`",
786786
"function", $f->getName()
787787
);
@@ -808,7 +808,7 @@ public function generateFunctionCall(Printer $printer, Func $f)
808808
$i++;
809809
}
810810
$printer->put(");\n");
811-
811+
$printer->put("GIT2_SHOULD_FREE(_`name`) = 0;\n", "name", $first->getName());
812812
});
813813
$printer->put("};\n");
814814

@@ -897,19 +897,15 @@ public function generateMakeResourceIfNeeded(Printer $printer, Func $f, $name =
897897
{
898898
if ($f->isResourceCreator() || $force) {
899899
$arg = $f->first();
900-
$printer->put("PHP_GIT2_MAKE_RESOURCE(`name`);\n", "name", $name);
901-
$printer->put("PHP_GIT2_V(`name`, `type`) = `variable`;\n",
902-
"name", $name,
903-
"type", $this->getNormarizedTypeName($arg),
904-
"variable", $arg->getName()
905-
);
906-
$printer->put("`name`->type = PHP_GIT2_TYPE_`type`;\n",
900+
$printer->put("if (php_git2_make_resource(&`name`, PHP_GIT2_TYPE_`type`, `name`, 1 TSRMLS_CC)) {\n",
907901
"name", $name,
908902
"type", strtoupper($this->getNormarizedTypeName($arg))
909903
);
910-
$printer->put("`name`->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);\n", "name", $name);
911-
$printer->put("`name`->should_free_v = 0;\n", "name", $name);
912-
$printer->put("ZVAL_RESOURCE(return_value, `name`->resource_id);\n", "name", $name);
904+
$printer->block(function(Printer $printer) {
905+
$printer->put("RETURN_FALSE;\n");
906+
});
907+
$printer->put("}\n");
908+
$printer->put("ZVAL_RESOURCE(return_value, GIT2_RVAL_P(`name`));\n", "name", $name);
913909
}
914910
}
915911

php_git2_priv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ extern int git2_resource_handle;
3535
#endif
3636

3737
#define PHP_GIT2_V(git2, type) git2->v.type
38+
#define GIT2_RVAL_P(git2) git2->resource_id
39+
#define GIT2_SHOULD_FREE(git2) git2->should_free_v
3840

3941
#define PHP_GIT2_MAKE_RESOURCE(val) \
4042
do {\

0 commit comments

Comments
 (0)