Skip to content

Commit 29dd0be

Browse files
committed
improve repository and revwalk
1 parent e5997aa commit 29dd0be

File tree

4 files changed

+112
-73
lines changed

4 files changed

+112
-73
lines changed

ng.php

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -456,73 +456,114 @@ public function isPtr(Arg $arg)
456456

457457
public function generateDeclarations(Printer $printer, Func $f)
458458
{
459+
$tables = array();
459460
if ($f->isResourceCreator()) {
460-
$printer->put("php_git2_t *result = NULL;\n");
461+
$tables["php_git2_t"][] = array(
462+
"name" => "*result",
463+
"value" => "NULL",
464+
);
461465
} else if ($f->isArrayCreator()) {
462-
$printer->put("`type` *result = NULL;\n", "type", $f->getReturnType());
466+
$tables[$f->getReturnType()][] = array(
467+
"name" => "*result",
468+
"value" => "NULL",
469+
);
463470
if (preg_match("/git_signature/", $f->getReturnType())) {
464-
$printer->put("zval *__result = NULL;\n");
471+
$tables["zval"][] = array(
472+
"name" => "*__result",
473+
"value" => "NULL",
474+
);
465475
}
466476
} else if ($f->isLongCreator()) {
467-
$printer->put("`type` result = 0;\n", "type", $f->getReturnType());
477+
$tables[$f->getReturnType()][] = array(
478+
"name" => "result",
479+
"value" => "0",
480+
);
468481
} else if ($f->isStringCreator()) {
469-
$printer->put("`type` *result = NULL;\n", "type", $f->getReturnType());
482+
$tables[$f->getReturnType()][] = array(
483+
"name" => "*result",
484+
"value" => "NULL",
485+
);
470486
}
471487

472488
$i = 0;
473489
$cnt = count($f->getArguments());
474490
foreach ($f->getArguments() as $arg) {
475491
/** @var Arg $arg */
476492
if ($i == 0 && $f->isResourceCreator()) {
477-
$printer->put("`type` `ptr``name` = NULL;\n",
478-
"type", $arg->getType(),
479-
"ptr", $arg->getPtr(),
480-
"name", $arg->getName()
493+
$tables[$arg->getType()][] = array(
494+
"name" => sprintf("%s%s", $arg->getPtr(), $arg->getName()),
495+
"value" => "NULL",
481496
);
482497
$i++;
483498
continue;
484499
}
485500
if ($arg->shouldWrite()) {
486-
$printer->put("`type` `ptr``name` = NULL;\n",
487-
"type", $arg->getType(),
488-
"ptr", $arg->getPtr(),
489-
"name", $arg->getName()
501+
$tables[$arg->getType()][] = array(
502+
"name" => sprintf("%s%s", $arg->getPtr(), $arg->getName()),
503+
"value" => "NULL",
490504
);
491505
} else {
492506
/** @var Arg $arg */
493-
$printer->put("`type` `ptr``name` = `value`;\n",
494-
"type", $arg->getZendType(),
495-
"ptr", $this->isPtr($arg),
496-
"name", $arg->getName(),
497-
"value", $arg->getDefaultValue()
507+
$tables[$arg->getZendType()][] = array(
508+
"name" => sprintf("%s%s", $this->isPtr($arg), $arg->getName()),
509+
"value" => $arg->getDefaultValue(),
498510
);
499511
if ($this->shouldResource($arg)) {
500-
$printer->put("`type` *_`name` = NULL;\n",
501-
"type", "php_git2_t",
502-
"name", $arg->getName()
512+
$tables["php_git2_t"][] = array(
513+
"name" => sprintf("*_%s", $arg->getName()),
514+
"value" => "NULL",
503515
);
504516
}
505517

506518
if (preg_match("/char/", $arg->getZendType())) {
507-
$printer->put("int `name`_len = 0;\n",
508-
"name", $arg->getName());
519+
$tables["int"][] = array(
520+
"name" => sprintf("%s_len", $arg->getName()),
521+
"value" => "0",
522+
);
509523
}
510524
}
511525

512526
if ($arg->getType() == "git_oid") {
513-
$printer->put("git_oid __`name`;\n", "name", $arg->getName());
527+
$tables["git_oid"][] = array(
528+
"name" => sprintf("__%s", $arg->getName()),
529+
"value" => "{0}",
530+
);
514531
}
515532

516533
$i++;
517534
}
518535
if ($f->getReturnType() == "int") {
519-
$printer->put("`type` error = 0;\n", "type", $f->getReturnType());
536+
$tables[$f->getReturnType()][] = array(
537+
"name" => "error",
538+
"value" => "0",
539+
);
520540
}
521541
if (preg_match("/git_oid/", $f->getReturnType())) {
522-
$printer->put("char __result[GIT2_OID_HEXSIZE] = {0};\n");
542+
$tables["char"][] = array(
543+
"name" => "__result[GIT2_OID_HEXSIZE]",
544+
"value" => "{0}",
545+
);
523546
}
524547
if (preg_match("/_owner$/", $f->getName())) {
525-
$printer->put("php_git2_t *__result = NULL;\n");
548+
$tables["php_git2_t"][] = array(
549+
"name" => "*__result",
550+
"value" => "NULL",
551+
);
552+
}
553+
554+
555+
foreach ($tables as $type => $values) {
556+
$printer->put("`type` ", "type", $type);
557+
$i = 0;
558+
$cnt = count($values);
559+
foreach ($values as $val) {
560+
$printer->put("`name` = `value`", "name", $val['name'], "value", $val["value"]);
561+
if ($i+1 < $cnt) {
562+
$printer->put(", ");
563+
}
564+
$i++;
565+
}
566+
$printer->put(";\n");
526567
}
527568
}
528569

@@ -897,9 +938,10 @@ public function generateMakeResourceIfNeeded(Printer $printer, Func $f, $name =
897938
{
898939
if ($f->isResourceCreator() || $force) {
899940
$arg = $f->first();
900-
$printer->put("if (php_git2_make_resource(&`name`, PHP_GIT2_TYPE_`type`, `name`, 1 TSRMLS_CC)) {\n",
941+
$printer->put("if (php_git2_make_resource(&`name`, PHP_GIT2_TYPE_`type`, `target`, 1 TSRMLS_CC)) {\n",
901942
"name", $name,
902-
"type", strtoupper($this->getNormarizedTypeName($arg))
943+
"type", strtoupper($this->getNormarizedTypeName($arg)),
944+
"target", "out"
903945
);
904946
$printer->block(function(Printer $printer) {
905947
$printer->put("RETURN_FALSE;\n");

php_git2.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,22 @@ void static destruct_git2(zend_rsrc_list_entry *rsrc TSRMLS_DC)
7373
break;
7474
case PHP_GIT2_TYPE_BLOB:
7575
git_blob_free(PHP_GIT2_V(resource, blob));
76+
break;
7677
case PHP_GIT2_TYPE_REVWALK:
7778
git_revwalk_free(PHP_GIT2_V(resource, revwalk));
79+
break;
7880
case PHP_GIT2_TYPE_TREEBUILDER:
7981
git_treebuilder_free(PHP_GIT2_V(resource, treebuilder));
82+
break;
8083
case PHP_GIT2_TYPE_REFERENCE:
8184
git_reference_free(PHP_GIT2_V(resource, reference));
85+
break;
8286
case PHP_GIT2_TYPE_CONFIG:
8387
git_config_free(PHP_GIT2_V(resource, config));
88+
break;
8489
case PHP_GIT2_TYPE_OBJECT:
8590
git_object_free(PHP_GIT2_V(resource, object));
91+
break;
8692
default:
8793
break;
8894
}

repository.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,31 @@ PHP_FUNCTION(git_repository_open_bare)
6969
}
7070

7171
/* {{{ proto resource git_repository_open(string $path)
72-
*/
72+
*/
7373
PHP_FUNCTION(git_repository_open)
7474
{
75-
char *path;
76-
int path_len;
77-
git_repository *repository;
78-
int error = 0;
79-
php_git2_t *git2;
75+
php_git2_t *result = NULL;
76+
git_repository *out = NULL;
77+
char *path = NULL;
78+
int path_len = 0, error = 0;
8079

8180
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
8281
"s", &path, &path_len) == FAILURE) {
8382
return;
8483
}
8584

86-
PHP_GIT2_MAKE_RESOURCE(git2);
87-
error = git_repository_open(&repository, path);
85+
error = git_repository_open(&out, path);
8886
if (php_git2_check_error(error, "git_repository_open" TSRMLS_CC)) {
89-
RETURN_FALSE
87+
RETURN_FALSE;
9088
}
91-
if (php_git2_make_resource(&git2, PHP_GIT2_TYPE_REPOSITORY, repository, 1 TSRMLS_CC)) {
89+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_REPOSITORY, out, 1 TSRMLS_CC)) {
9290
RETURN_FALSE;
9391
}
94-
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(git2));
92+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
9593
}
9694
/* }}} */
9795

96+
9897
/* {{{ proto string git_repository_get_namespace(resource $repository)
9998
*/
10099
PHP_FUNCTION(git_repository_get_namespace)

revwalk.c

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#include "php_git2_priv.h"
33
#include "revwalk.h"
44

5-
/* {{{ proto resource git_revwalk_new(repo)
6-
*/
5+
/* {{{ proto resource git_revwalk_new(resource $repo)
6+
*/
77
PHP_FUNCTION(git_revwalk_new)
88
{
9-
zval *repo;
10-
php_git2_t *_repo, *result;
11-
git_revwalk *walker;
9+
php_git2_t *result = NULL, *_repo = NULL;
10+
git_revwalk *out = NULL;
11+
zval *repo = NULL;
1212
int error = 0;
1313

1414
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
@@ -17,19 +17,17 @@ PHP_FUNCTION(git_revwalk_new)
1717
}
1818

1919
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
20-
error = git_revwalk_new(&walker, PHP_GIT2_V(_repo, repository));
20+
error = git_revwalk_new(&out, PHP_GIT2_V(_repo, repository));
2121
if (php_git2_check_error(error, "git_revwalk_new" TSRMLS_CC)) {
22-
RETURN_FALSE
22+
RETURN_FALSE;
2323
}
24-
25-
PHP_GIT2_MAKE_RESOURCE(result);
26-
PHP_GIT2_V(result, revwalk) = walker;
27-
result->type = PHP_GIT2_TYPE_REVWALK;
28-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
29-
result->should_free_v = 1;
30-
31-
ZVAL_RESOURCE(return_value, result->resource_id);
24+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_REVWALK, out, 1 TSRMLS_CC)) {
25+
RETURN_FALSE;
26+
}
27+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
3228
}
29+
/* }}} */
30+
3331

3432
/* {{{ proto void git_revwalk_reset(walker)
3533
*/
@@ -47,35 +45,30 @@ PHP_FUNCTION(git_revwalk_reset)
4745
git_revwalk_reset(PHP_GIT2_V(_walker, revwalk));
4846
}
4947

50-
/* {{{ proto long git_revwalk_push(walk, id)
51-
*/
48+
/* {{{ proto long git_revwalk_push(resource $walk, string $id)
49+
*/
5250
PHP_FUNCTION(git_revwalk_push)
5351
{
54-
zval *walk;
55-
php_git2_t *_walk;
56-
char *id = {0};
57-
int id_len;
58-
git_oid oid;
59-
int error = 0;
52+
int result = 0, id_len = 0;
53+
zval *walk = NULL;
54+
php_git2_t *_walk = NULL;
55+
char *id = NULL;
56+
git_oid __id = {0};
6057

6158
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
6259
"rs", &walk, &id, &id_len) == FAILURE) {
6360
return;
6461
}
6562

66-
if (git_oid_fromstrn(&oid, id, id_len) != GIT_OK) {
67-
return;
68-
}
69-
7063
ZEND_FETCH_RESOURCE(_walk, php_git2_t*, &walk, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
71-
error = git_revwalk_push(PHP_GIT2_V(_walk, revwalk), &oid);
72-
73-
if (php_git2_check_error(error, "git_revwalk_push" TSRMLS_CC)) {
64+
if (git_oid_fromstrn(&__id, id, id_len)) {
7465
RETURN_FALSE;
75-
} else {
76-
RETURN_TRUE;
7766
}
67+
result = git_revwalk_push(PHP_GIT2_V(_walk, revwalk), &__id);
68+
RETURN_LONG(result);
7869
}
70+
/* }}} */
71+
7972

8073
/* {{{ proto long git_revwalk_push_glob(walk, glob)
8174
*/
@@ -248,7 +241,6 @@ PHP_FUNCTION(git_revwalk_next)
248241

249242
ZEND_FETCH_RESOURCE(_walk, php_git2_t*, &walk, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
250243
error = git_revwalk_next(&id, PHP_GIT2_V(_walk, revwalk));
251-
252244
if (error == GIT_ITEROVER) {
253245
RETURN_FALSE;
254246
}

0 commit comments

Comments
 (0)