Skip to content

Commit e20f10d

Browse files
committed
add giterr
1 parent 34ab9e7 commit e20f10d

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

config.m4

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ PHP_ARG_ENABLE(git2-debug, for git2 debug support,
77
if test $PHP_GIT2 != "no"; then
88
PHP_SUBST(GIT2_SHARED_LIBADD)
99

10-
PHP_NEW_EXTENSION(git2, php_git2.c repository.c commit.c tree.c clone.c blob.c helper.c revwalk.c treebuilder.c reference.c g_config.c object.c index.c revparse.c branch.c tag.c status.c cred.c remote.c transport.c diff.c checkout.c filter.c ignore.c indexer.c pathspec.c patch.c merge.c note.c odb.c reflog.c blame.c packbuilder.c stash.c signature.c attr.c reset.c message.c submodule.c, $ext_shared)
10+
PHP_NEW_EXTENSION(git2, php_git2.c repository.c commit.c tree.c clone.c blob.c helper.c revwalk.c treebuilder.c reference.c g_config.c object.c index.c revparse.c branch.c tag.c status.c cred.c remote.c transport.c diff.c checkout.c filter.c ignore.c indexer.c pathspec.c patch.c merge.c note.c odb.c reflog.c blame.c packbuilder.c stash.c signature.c attr.c reset.c message.c submodule.c giterr.c, $ext_shared)
1111
PHP_ADD_INCLUDE([$ext_srcdir/libgit2/include])
1212

1313
# for now

giterr.c

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "php_git2.h"
2+
#include "php_git2_priv.h"
3+
#include "giterr.h"
4+
5+
static void php_git2_error_to_array(const git_error *error, zval **out)
6+
{
7+
zval *array;
8+
9+
MAKE_STD_ZVAL(array);
10+
array_init(array);
11+
if (error == NULL) {
12+
add_next_index_string(array, "", 1);
13+
add_next_index_long(array, 0);
14+
} else {
15+
add_next_index_string(array, error->message, 1);
16+
add_next_index_long(array, error->klass);
17+
}
18+
*out = array;
19+
}
20+
21+
/* {{{ proto resource giterr_last()
22+
*/
23+
PHP_FUNCTION(giterr_last)
24+
{
25+
const git_error *result = {0};
26+
zval *array;
27+
28+
result = giterr_last();
29+
php_git2_error_to_array(result, &array);
30+
31+
RETURN_ZVAL(array, 0, 1);
32+
}
33+
/* }}} */
34+
35+
/* {{{ proto void giterr_clear()
36+
*/
37+
PHP_FUNCTION(giterr_clear)
38+
{
39+
giterr_clear();
40+
}
41+
/* }}} */
42+
43+
/* {{{ proto array giterr_detach()
44+
*/
45+
PHP_FUNCTION(giterr_detach)
46+
{
47+
int result = 0, error = 0;
48+
git_error cpy;
49+
zval *array;
50+
51+
result = giterr_detach(&cpy);
52+
php_git2_error_to_array(&cpy, &array);
53+
RETURN_ZVAL(array, 0, 1);
54+
}
55+
/* }}} */
56+
57+
/* {{{ proto void giterr_set_str(long $error_class, string $string)
58+
*/
59+
PHP_FUNCTION(giterr_set_str)
60+
{
61+
long error_class = 0;
62+
char *string = NULL;
63+
int string_len = 0;
64+
65+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
66+
"ls", &error_class, &string, &string_len) == FAILURE) {
67+
return;
68+
}
69+
70+
giterr_set_str(error_class, string);
71+
}
72+
/* }}} */
73+
74+
/* {{{ proto void giterr_set_oom()
75+
*/
76+
PHP_FUNCTION(giterr_set_oom)
77+
{
78+
giterr_set_oom();
79+
}
80+
/* }}} */
81+

giterr.h

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* PHP Libgit2 Extension
3+
*
4+
* https://github.com/libgit2/php-git
5+
*
6+
* Copyright 2014 Shuhei Tanuma. All rights reserved.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef PHP_GIT2_GITERR_H
27+
#define PHP_GIT2_GITERR_H
28+
29+
ZEND_BEGIN_ARG_INFO_EX(arginfo_giterr_last, 0, 0, 1)
30+
ZEND_ARG_INFO(0, void)
31+
ZEND_END_ARG_INFO()
32+
33+
ZEND_BEGIN_ARG_INFO_EX(arginfo_giterr_clear, 0, 0, 1)
34+
ZEND_ARG_INFO(0, void)
35+
ZEND_END_ARG_INFO()
36+
37+
ZEND_BEGIN_ARG_INFO_EX(arginfo_giterr_detach, 0, 0, 1)
38+
ZEND_ARG_INFO(0, cpy)
39+
ZEND_END_ARG_INFO()
40+
41+
ZEND_BEGIN_ARG_INFO_EX(arginfo_giterr_set_str, 0, 0, 2)
42+
ZEND_ARG_INFO(0, error_class)
43+
ZEND_ARG_INFO(0, string)
44+
ZEND_END_ARG_INFO()
45+
46+
ZEND_BEGIN_ARG_INFO_EX(arginfo_giterr_set_oom, 0, 0, 1)
47+
ZEND_ARG_INFO(0, void)
48+
ZEND_END_ARG_INFO()
49+
50+
/* {{{ proto resource giterr_last(void)
51+
*/
52+
PHP_FUNCTION(giterr_last);
53+
54+
/* {{{ proto void giterr_clear(void)
55+
*/
56+
PHP_FUNCTION(giterr_clear);
57+
58+
/* {{{ proto long giterr_detach(cpy)
59+
*/
60+
PHP_FUNCTION(giterr_detach);
61+
62+
/* {{{ proto void giterr_set_str(error_class, string)
63+
*/
64+
PHP_FUNCTION(giterr_set_str);
65+
66+
/* {{{ proto void giterr_set_oom(void)
67+
*/
68+
PHP_FUNCTION(giterr_set_oom);
69+
70+
#endif

php_git2.c

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "message.h"
6363
#include "submodule.h"
6464
#include "attr.h"
65+
#include "giterr.h"
6566

6667
int git2_resource_handle;
6768

@@ -852,6 +853,13 @@ static zend_function_entry php_git2_functions[] = {
852853
PHP_FE(git_attr_cache_flush, arginfo_git_attr_cache_flush)
853854
PHP_FE(git_attr_add_macro, arginfo_git_attr_add_macro)
854855

856+
/* giterr */
857+
PHP_FE(giterr_last, arginfo_giterr_last)
858+
PHP_FE(giterr_clear, arginfo_giterr_clear)
859+
PHP_FE(giterr_detach, arginfo_giterr_detach)
860+
PHP_FE(giterr_set_str, arginfo_giterr_set_str)
861+
PHP_FE(giterr_set_oom, arginfo_giterr_set_oom)
862+
855863
/* misc */
856864
PHP_FE(git_resource_type, arginfo_git_resource_type)
857865
PHP_FE_END

0 commit comments

Comments
 (0)