Skip to content

Commit e9bcab4

Browse files
committed
added git2_cred (just a start)
1 parent 7a3e8fb commit e9bcab4

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ if test $PHP_GIT2 != "no"; then
2020
PHP_ADD_LIBRARY_WITH_PATH(git2, libgit2/build, GIT2_SHARED_LIBADD)
2121
fi
2222

23-
PHP_NEW_EXTENSION(git2, php_git2.c git2_exception.c git2_repository.c git2_config.c git2_config_entry.c git2_reference.c git2_commit.c git2_remote.c git2_tree.c git2_tree_entry.c git2_blob.c git2_php_util.c, $ext_shared)
23+
PHP_NEW_EXTENSION(git2, php_git2.c git2_php_util.c git2_exception.c git2_repository.c git2_config.c git2_config_entry.c git2_reference.c git2_commit.c git2_remote.c git2_tree.c git2_tree_entry.c git2_blob.c git2_cred.c, $ext_shared)
2424
PHP_SUBST(GIT2_SHARED_LIBADD)
2525
fi

git2_cred.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "php_git2.h"
2+
#include "git2_exception.h"
3+
#include "git2_cred.h"
4+
#include "git2_php_util.h"
5+
6+
static zend_class_entry *php_git2_cred_ce;
7+
static zend_object_handlers php_git2_cred_handler;
8+
9+
typedef struct _git2_cred_object {
10+
zend_object std;
11+
git_cred *cred;
12+
} git2_cred_object_t;
13+
14+
ZEND_BEGIN_ARG_INFO_EX(arginfo_cred_create_plaintext, 0, 0, 2)
15+
ZEND_ARG_INFO(0, username)
16+
ZEND_ARG_INFO(0, password)
17+
ZEND_END_ARG_INFO()
18+
19+
static PHP_METHOD(Cred, create_plaintext) {
20+
char *username, *password;
21+
size_t username_len, password_len;
22+
git2_cred_object_t *intern;
23+
24+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &username, &username_len, &password, &password_len) == FAILURE)
25+
return;
26+
27+
object_init_ex(return_value, php_git2_cred_ce);
28+
intern = (git2_cred_object_t*)Z_OBJ_P(return_value);
29+
int res = git_cred_userpass_plaintext_new(&intern->cred, username, password);
30+
31+
if (res != 0) {
32+
git2_throw_last_error();
33+
return;
34+
}
35+
}
36+
37+
#define GIT2_CRED_FETCH() git2_cred_object_t *intern = (git2_cred_object_t*)Z_OBJ_P(getThis()); \
38+
if (intern->cred == NULL) { \
39+
git2_throw_exception(0 TSRMLS_CC, "Git2\\Cred object in invalid state"); \
40+
return; \
41+
}
42+
43+
#define GIT2_CRED_GET_BOOL(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_cred_ ## _x, 0, 0, 0) \
44+
ZEND_END_ARG_INFO() \
45+
static PHP_METHOD(Cred, _x) { \
46+
if (zend_parse_parameters_none() == FAILURE) return; \
47+
GIT2_CRED_FETCH(); \
48+
RETURN_BOOL(git_cred_ ## _x(intern->cred)); \
49+
}
50+
51+
GIT2_CRED_GET_BOOL(has_username)
52+
53+
void git2_cred_spawn(zval *return_value, git_cred *cred TSRMLS_DC) {
54+
git2_cred_object_t *intern;
55+
56+
object_init_ex(return_value, php_git2_cred_ce);
57+
intern = (git2_cred_object_t*)Z_OBJ_P(return_value);
58+
intern->cred = cred;
59+
}
60+
61+
zend_object *php_git2_cred_create_object(zend_class_entry *class_type TSRMLS_DC) {
62+
git2_cred_object_t *intern = NULL;
63+
64+
intern = emalloc(sizeof(git2_cred_object_t));
65+
memset(intern, 0, sizeof(git2_cred_object_t));
66+
67+
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
68+
object_properties_init(&intern->std, class_type);
69+
70+
intern->std.handlers = &php_git2_cred_handler;
71+
72+
return &intern->std;
73+
}
74+
75+
static void php_git2_cred_free_object(zend_object *object TSRMLS_DC) {
76+
git2_cred_object_t *intern = (git2_cred_object_t*)object;
77+
78+
zend_object_std_dtor(&intern->std TSRMLS_CC);
79+
80+
if (intern->cred) {
81+
git_cred_free(intern->cred);
82+
intern->cred = NULL;
83+
}
84+
85+
// no need with PHP7 to free intern
86+
}
87+
88+
#define PHP_GIT2_CRED_ME_P(_x) PHP_ME(Cred, _x, arginfo_cred_##_x, ZEND_ACC_PUBLIC)
89+
90+
static zend_function_entry git2_cred_methods[] = {
91+
PHP_ME(Cred, create_plaintext, arginfo_cred_create_plaintext, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
92+
PHP_GIT2_CRED_ME_P(has_username)
93+
/* PHP_ME(Cred, __construct, arginfo___construct, ZEND_ACC_PUBLIC) */
94+
{ NULL, NULL, NULL }
95+
};
96+
97+
void git2_cred_init(TSRMLS_D) {
98+
zend_class_entry ce;
99+
100+
INIT_NS_CLASS_ENTRY(ce, "Git2", "Cred", git2_cred_methods);
101+
php_git2_cred_ce = zend_register_internal_class(&ce TSRMLS_CC);
102+
php_git2_cred_ce->create_object = php_git2_cred_create_object;
103+
104+
memcpy(&php_git2_cred_handler, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
105+
php_git2_cred_handler.clone_obj = NULL;
106+
php_git2_cred_handler.free_obj = php_git2_cred_free_object;
107+
108+
#define GIT2_CRED_CONST(_x) zend_declare_class_constant_long(php_git2_cred_ce, ZEND_STRL(#_x), GIT_CRED ## _x TSRMLS_CC)
109+
110+
GIT2_CRED_CONST(TYPE_USERPASS_PLAINTEXT);
111+
GIT2_CRED_CONST(TYPE_SSH_KEY);
112+
GIT2_CRED_CONST(TYPE_SSH_CUSTOM);
113+
GIT2_CRED_CONST(TYPE_DEFAULT);
114+
GIT2_CRED_CONST(TYPE_SSH_INTERACTIVE);
115+
GIT2_CRED_CONST(TYPE_USERNAME);
116+
GIT2_CRED_CONST(TYPE_SSH_MEMORY);
117+
}
118+

git2_cred.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef GIT2_CRED_H
2+
#define GIT2_CRED_H
3+
4+
void git2_cred_init(TSRMLS_DC);
5+
6+
void git2_cred_spawn(zval *return_value, git_cred *cred TSRMLS_DC);
7+
8+
#endif /* GIT2_CRED_H */

php_git2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "git2_tree.h"
1111
#include "git2_tree_entry.h"
1212
#include "git2_blob.h"
13+
#include "git2_cred.h"
1314

1415
static zend_class_entry *php_git2_base_ce;
1516
static zend_object_handlers php_git2_base_handler;
@@ -74,6 +75,7 @@ PHP_MINIT_FUNCTION(git2) {
7475
git2_tree_init(TSRMLS_C);
7576
git2_tree_entry_init(TSRMLS_C);
7677
git2_blob_init(TSRMLS_C);
78+
git2_cred_init(TSRMLS_C);
7779

7880
return SUCCESS;
7981
}

0 commit comments

Comments
 (0)