for now, added commit, tree, blob classes · libgit2/php-git@21bf947 · GitHub
Skip to content

Commit 21bf947

Browse files
committed
for now, added commit, tree, blob classes
1 parent 6b72f93 commit 21bf947

10 files changed

Lines changed: 347 additions & 2 deletions

‎blob.c‎

Lines changed: 59 additions & 0 deletions

‎commit.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2010 - 2012 Shuhei Tanuma
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include "php_git2.h"
26+
27+
PHPAPI zend_class_entry *git2_commit_class_entry;
28+
29+
static void php_git2_commit_free_storage(php_git2_commit *object TSRMLS_DC)
30+
{
31+
if (object->commit != NULL) {
32+
git_commit_free(object->commit);
33+
object->commit = NULL;
34+
}
35+
zend_object_std_dtor(&object->zo TSRMLS_CC);
36+
efree(object);
37+
}
38+
39+
zend_object_value php_git2_commit_new(zend_class_entry *ce TSRMLS_DC)
40+
{
41+
zend_object_value retval;
42+
43+
PHP_GIT2_STD_CREATE_OBJECT(php_git2_commit);
44+
return retval;
45+
}
46+
47+
48+
static zend_function_entry php_git2_commit_methods[] = {
49+
{NULL,NULL,NULL}
50+
};
51+
52+
void php_git2_commit_init(TSRMLS_D)
53+
{
54+
zend_class_entry ce;
55+
56+
INIT_NS_CLASS_ENTRY(ce, PHP_GIT2_NS, "Commit", php_git2_commit_methods);
57+
git2_commit_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
58+
git2_commit_class_entry->create_object = php_git2_commit_new;
59+
}

‎config.m4‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ if test $PHP_GIT2 != "no"; then
3131
PHP_SUBST(GIT_SHARED_LIBADD)
3232
PHP_NEW_EXTENSION(git2, git2.c \
3333
repository.c \
34+
commit.c \
35+
blob.c \
36+
tree.c \
3437
, $ext_shared)
3538

3639
ifdef([PHP_ADD_EXTENSION_DEP],

‎git2.c‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,49 @@
2525
#include "php_git2.h"
2626

2727
extern void php_git2_repository_init(TSRMLS_D);
28+
extern void php_git2_commit_init(TSRMLS_D);
29+
extern void php_git2_blob_init(TSRMLS_D);
30+
extern void php_git2_tree_init(TSRMLS_D);
31+
32+
zval* php_git2_object_new(php_git2_repository *repository, git_object *object TSRMLS_DC)
33+
{
34+
zval *result = NULL;
35+
MAKE_STD_ZVAL(result);
36+
37+
switch (git_object_type(object)) {
38+
case GIT_OBJ_COMMIT: {
39+
php_git2_commit *m_obj = NULL;
40+
41+
object_init_ex(result, git2_commit_class_entry);
42+
m_obj = PHP_GIT2_GET_OBJECT(php_git2_commit, result);
43+
m_obj->commit = (git_commit*)object;
44+
break;
45+
}
46+
case GIT_OBJ_BLOB: {
47+
php_git2_blob *m_obj = NULL;
48+
49+
object_init_ex(result, git2_blob_class_entry);
50+
m_obj = PHP_GIT2_GET_OBJECT(php_git2_blob, result);
51+
m_obj->blob = (git_blob*)object;
52+
break;
53+
}
54+
case GIT_OBJ_TREE: {
55+
php_git2_tree *m_obj = NULL;
56+
57+
object_init_ex(result, git2_tree_class_entry);
58+
m_obj = PHP_GIT2_GET_OBJECT(php_git2_tree, result);
59+
m_obj->tree = (git_tree*)object;
60+
break;
61+
}
62+
case GIT_OBJ_TAG: {
63+
break;
64+
}
65+
default:
66+
break;
67+
}
68+
69+
return result;
70+
}
2871

2972
int php_git2_add_protected_property_string_ex(zval *object, char *name, int name_length, char *data, zend_bool duplicate TSRMLS_DC)
3073
{
@@ -45,6 +88,9 @@ int php_git2_add_protected_property_string_ex(zval *object, char *name, int name
4588
PHP_MINIT_FUNCTION(git2)
4689
{
4790
php_git2_repository_init(TSRMLS_C);
91+
php_git2_commit_init(TSRMLS_C);
92+
php_git2_blob_init(TSRMLS_C);
93+
php_git2_tree_init(TSRMLS_C);
4894

4995
return SUCCESS;
5096
}

‎php_git2.h‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,29 @@ extern zend_module_entry git2_module_entry;
4343
# define PHP_GIT2_NS "Git2"
4444

4545
extern PHPAPI zend_class_entry *git2_repository_class_entry;
46+
extern PHPAPI zend_class_entry *git2_commit_class_entry;
47+
extern PHPAPI zend_class_entry *git2_blob_class_entry;
48+
extern PHPAPI zend_class_entry *git2_tree_class_entry;
4649

4750
typedef struct{
4851
zend_object zo;
4952
git_repository *repository;
5053
} php_git2_repository;
5154

55+
typedef struct{
56+
zend_object zo;
57+
git_commit *commit;
58+
} php_git2_commit;
59+
60+
typedef struct{
61+
zend_object zo;
62+
git_blob *blob;
63+
} php_git2_blob;
64+
65+
typedef struct{
66+
zend_object zo;
67+
git_tree *tree;
68+
} php_git2_tree;
5269

5370
# define PHP_GIT2_GET_OBJECT(STRUCT_NAME, OBJECT) (STRUCT_NAME *) zend_object_store_get_object(OBJECT TSRMLS_CC);
5471

@@ -81,6 +98,7 @@ typedef struct{
8198
retval.handlers = zend_get_std_object_handlers();
8299
# endif
83100

84-
static int php_git2_add_protected_property_string_ex(zval *object, char *name, int name_length, char *data, zend_bool duplicate TSRMLS_DC);
101+
extern int php_git2_add_protected_property_string_ex(zval *object, char *name, int name_length, char *data, zend_bool duplicate TSRMLS_DC);
102+
extern zval* php_git2_object_new(php_git2_repository *repository, git_object *object TSRMLS_DC);
85103

86104
#endif /* PHP_GIT2_H */

‎repository.c‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository_exists, 0,0,1)
6363
ZEND_ARG_INFO(0, exists)
6464
ZEND_END_ARG_INFO()
6565

66+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository_lookup, 0,0,1)
67+
ZEND_ARG_INFO(0, lookup)
68+
ZEND_ARG_INFO(0, type)
69+
ZEND_END_ARG_INFO()
70+
6671
/*
6772
{{{ proto: Git2\Repsotiroy::__construct(string $path)
6873
*/
@@ -283,7 +288,7 @@ PHP_METHOD(git2_repository, discover)
283288
return;
284289
}
285290

286-
if (git_repository_discover(&path_buffer,path_size,start_path,(int)across_fs, ceiling_dirs) == GIT_SUCCESS) {
291+
if (git_repository_discover(path_buffer,path_size,(const char *)start_path,(int)across_fs, (const char *)ceiling_dirs) == GIT_SUCCESS) {
287292
RETVAL_STRING(path_buffer, 1);
288293
} else {
289294
RETURN_FALSE;
@@ -326,6 +331,47 @@ PHP_METHOD(git2_repository, exists)
326331
/* }}} */
327332

328333

334+
/*
335+
{{{ proto: Git2\Repsotiroy::lookup(string $sha1[, int type = GIT_OBJ_ANY])
336+
*/
337+
PHP_METHOD(git2_repository, lookup)
338+
{
339+
char *hash;
340+
int error, hash_len = 0;
341+
git_odb *odb;
342+
git_oid id;
343+
git_object *object;
344+
long type = GIT_OBJ_ANY;
345+
php_git2_repository *m_repository;
346+
347+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
348+
"s|l", &hash, &hash_len, type) == FAILURE) {
349+
return;
350+
}
351+
352+
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
353+
if (git_repository_odb(&odb, m_repository->repository) == GIT_SUCCESS) {
354+
zval *result = NULL;
355+
356+
if (git_oid_fromstrn(&id, hash, hash_len) != GIT_SUCCESS) {
357+
RETURN_FALSE;
358+
}
359+
360+
if (hash_len < GIT_OID_HEXSZ) {
361+
error = git_object_lookup_prefix(&object, m_repository->repository, &id, hash_len, (git_otype)type);
362+
} else {
363+
error = git_object_lookup(&object, m_repository->repository, &id, (git_otype)type);
364+
}
365+
366+
result = php_git2_object_new(m_repository, object TSRMLS_CC);
367+
RETVAL_ZVAL(result,0,1);
368+
} else {
369+
/* @todo: throws an exception */
370+
RETURN_FALSE;
371+
}
372+
}
373+
/* }}} */
374+
329375

330376
static zend_function_entry php_git2_repository_methods[] = {
331377
PHP_ME(git2_repository, __construct, arginfo_git2_repository___construct, ZEND_ACC_PUBLIC)
@@ -338,6 +384,13 @@ static zend_function_entry php_git2_repository_methods[] = {
338384
PHP_ME(git2_repository, init, arginfo_git2_repository_init, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
339385
PHP_ME(git2_repository, discover, arginfo_git2_repository_discover, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
340386
PHP_ME(git2_repository, exists, arginfo_git2_repository_exists, ZEND_ACC_PUBLIC)
387+
#ifdef lookup
388+
#undef lookup
389+
#endif
390+
PHP_ME(git2_repository, lookup, arginfo_git2_repository_lookup, ZEND_ACC_PUBLIC)
391+
#ifndef lookup
392+
#define lookup php_lookup
393+
#endif
341394
{NULL,NULL,NULL}
342395
};
343396

‎tests/002-01-commit_construct.phpt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Check for Git2\Commit::construct
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$repo = new Git2\Repository(__DIR__ . "/mock/001-01");
8+
$commit = $repo->lookup("ab68c54212af15d3545c41057e3a8f2f9ff6fd0d");
9+
10+
if ($commit instanceof Git2\Commit) {
11+
echo "OK" . PHP_EOL;
12+
} else {
13+
echo "NG" . PHP_EOL;
14+
}
15+
--EXPECT--
16+
OK

‎tests/003-01-tree_construct.phpt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Check for Git2\Tree::construct
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$repo = new Git2\Repository(__DIR__ . "/mock/001-01");
8+
$tree = $repo->lookup("67dc4302383b2715f4e0b8c41840eb05b1873697");
9+
10+
if ($tree instanceof Git2\Tree) {
11+
echo "OK" . PHP_EOL;
12+
} else {
13+
echo "NG" . PHP_EOL;
14+
}
15+
--EXPECT--
16+
OK

‎tests/004-01-blob_construct.phpt‎

Lines changed: 16 additions & 0 deletions

0 commit comments

Comments
 (0)