Skip to content

Commit 54a5bed

Browse files
committed
Updated git push example.
Updated Library config file to wrap needed functions/structs/enums and callbacks. Added new generated Eiffel code. Added a new manual wrapping GIT_REMOTE to wrap GIT_REMOTE functions.
1 parent 1914c94 commit 54a5bed

23 files changed

+3398
-5
lines changed

examples/push/application.e

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
note
22
description: "[
3-
libgit2 "push" example - shows how to git push <remote> <branch>
3+
libgit2 "push" example - shows how to git push <remote> <branch>
44
]"
55

66
class APPLICATION
@@ -43,6 +43,9 @@ feature -- Intiialize Repository
4343
refspec: STRING
4444
l_options: GIT_PUSH_OPTIONS_STRUCT_API
4545
a_array: GIT_STRARRAY_STRUCT_API
46+
callbacks: GIT_REMOTE_CALLBACKS_STRUCT_API
47+
callback_dispatcher: GIT_CRED_ACQUIRE_CB_DISPATCHER
48+
4649
do
4750
ini := {LIBGIT2_INITIALIZER_API}.git_libgit2_init
4851
print ("%N Intializing Libgit2")
@@ -56,14 +59,24 @@ feature -- Intiialize Repository
5659

5760
-- get the remote
5861
create l_remote.make
62+
5963
create git_remote
6064
if git_remote.git_remote_lookup (l_remote, repo, remote) < 0 then
6165
print ("%NCould not get remote repository " + remote)
6266
{EXCEPTIONS}.die (1)
6367
end
6468

69+
create callbacks.make
70+
if git_remote.git_remote_init_callbacks (callbacks, 1) < 0 then
71+
print ("%NCould not intialize callback ")
72+
{EXCEPTIONS}.die (1)
73+
end
74+
75+
create callback_dispatcher.make (agent cred_acquire_cb )
76+
callbacks.set_credentials (callback_dispatcher.c_dispatcher)
77+
6578
-- connect to remote
66-
if git_remote.git_remote_connect (l_remote, {GIT_DIRECTION_ENUM_API}.git_direction_push, Void, Void, Void) < 0 then
79+
if git_remote.git_remote_connect (l_remote, {GIT_DIRECTION_ENUM_API}.git_direction_push, callbacks, Void, Void) < 0 then
6780
print ("%NCould not connect to remote repository " + remote)
6881
{EXCEPTIONS}.die (1)
6982
end
@@ -112,8 +125,62 @@ feature -- Intiialize Repository
112125
end
113126

114127

115-
feature {NONE} -- Process Arguments
116128

129+
cred_acquire_cb (a_cred: POINTER; a_url: POINTER; a_username_from_url: POINTER; a_allowed_types: INTEGER; a_payload: POINTER): INTEGER
130+
local
131+
l_user_name: STRING
132+
exit: BOOLEAN
133+
cred: GIT_CRED_STRUCT_API
134+
git_cred: GIT_CREDENTIALS_API
135+
l_password: STRING
136+
l_privkey: STRING
137+
l_pubkey: STRING
138+
do
139+
140+
if a_username_from_url /= default_pointer then
141+
l_user_name := (create {C_STRING}.make_by_pointer (a_username_from_url)).string
142+
exit := l_user_name.is_empty
143+
else
144+
print ("%NUsername:")
145+
io.read_line
146+
l_user_name := io.last_string.twin
147+
exit := l_user_name.is_empty
148+
end
149+
150+
if not exit and then a_allowed_types & {GIT_CREDTYPE_T_ENUM_API}.GIT_CREDTYPE_SSH_KEY > 0 then
151+
print ("%NSSH key:")
152+
io.read_line
153+
l_privkey := io.last_string.twin
154+
print ("%NPassword:")
155+
l_password := read_password
156+
if l_password.is_empty or l_privkey.is_empty then
157+
exit := True
158+
end
159+
create l_pubkey.make_from_string (l_password)
160+
l_pubkey.append_string (".pub")
161+
create git_cred
162+
create cred.make_by_pointer (a_cred)
163+
Result := git_cred.git_cred_ssh_key_new(cred, l_user_name, l_pubkey, l_privkey, l_password)
164+
elseif not exit and then a_allowed_types & {GIT_CREDTYPE_T_ENUM_API}.GIT_CREDTYPE_USERPASS_PLAINTEXT > 0 then
165+
print ("%NPassword:")
166+
l_password := read_password
167+
exit := l_password.is_empty
168+
if not exit then
169+
create git_cred
170+
create cred.make_by_pointer (a_cred)
171+
Result := git_cred.git_cred_userpass_plaintext_new(cred, l_user_name, l_password)
172+
end
173+
else
174+
if not exit then
175+
create git_cred
176+
create cred.make_by_pointer (a_cred)
177+
Result := git_cred.git_cred_username_new (cred, l_user_name)
178+
end
179+
end
180+
181+
end
182+
183+
feature {NONE} -- Process Arguments
117184

118185
process_arguments
119186
-- Process command line arguments
@@ -153,7 +220,6 @@ feature {NONE} -- Process Arguments
153220
str: STRING
154221
do
155222
str := "[
156-
%N
157223
git_push [--git-dir=<directory>]
158224
<remote>
159225
<branch>
@@ -163,6 +229,53 @@ feature {NONE} -- Process Arguments
163229
print (str)
164230
end
165231

232+
read_password: STRING
233+
local
234+
l_ptr: POINTER
235+
do
236+
l_ptr := c_read_password
237+
if l_ptr /= default_pointer then
238+
Result := (create {C_STRING}.make_by_pointer (l_ptr)).string
239+
else
240+
Result := ""
241+
end
242+
end
243+
244+
c_read_password: POINTER
245+
external "C inline"
246+
alias
247+
"[
248+
#define ENTER 13
249+
#define TAB 9
250+
#define BKSP 8
251+
252+
char* pwd;
253+
254+
int i = 0;
255+
char ch;
256+
257+
while(1){
258+
ch = getch(); //get key
259+
260+
if(ch == ENTER || ch == TAB){
261+
pwd[i] = '\0';
262+
break;
263+
}else if(ch == BKSP){
264+
if(i > 0){
265+
i--;
266+
printf("\b \b"); //for backspace
267+
}
268+
}else{
269+
pwd[i++] = ch;
270+
printf("* \b"); //to replace password character with *
271+
}
272+
}//while ends here
273+
274+
return pwd;
275+
]"
276+
end
277+
278+
166279
feature -- Options
167280

168281
path: STRING

library/config.xml

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,81 @@
538538
</wrapper>
539539
</rule>
540540

541+
<!-- The next rule specify the Remote functions -->
542+
<rule>
543+
<match>
544+
<identifier name="git_remote.*"/>
545+
<type name="function"/>
546+
</match>
547+
<wrapper type="default">
548+
<class_name name="GIT_REMOTE"/>
549+
</wrapper>
550+
</rule>
551+
552+
<!-- The next rule specify the Remote functions -->
553+
<rule>
554+
<match>
555+
<identifier name="git_push_init_options"/>
556+
<type name="function"/>
557+
</match>
558+
<wrapper type="default">
559+
<class_name name="GIT_REMOTE"/>
560+
</wrapper>
561+
</rule>
562+
563+
<!-- The next rule specify the Remote enum -->
564+
<rule>
565+
<match>
566+
<identifier name="git_remote.*"/>
567+
<type name="enum"/>
568+
</match>
569+
<wrapper type="default">
570+
</wrapper>
571+
</rule>
572+
573+
574+
<!-- The next rule specify the credential functions -->
575+
<rule>
576+
<match>
577+
<identifier name="git_cred.*"/>
578+
<type name="function"/>
579+
</match>
580+
<wrapper type="default">
581+
<class_name name="GIT_CREDENTIALS"/>
582+
</wrapper>
583+
</rule>
584+
585+
<!-- The next rule specify the credential struct -->
586+
<rule>
587+
<match>
588+
<identifier name="git_cred.*"/>
589+
<type name="struct"/>
590+
</match>
591+
<wrapper type="default">
592+
</wrapper>
593+
</rule>
594+
595+
<!-- The next rule specify the git_cred_acquire_cb callback -->
596+
<rule>
597+
<match>
598+
<identifier name="git_cred_acquire_cb"/>
599+
</match>
600+
<wrapper type="default">
601+
</wrapper>
602+
</rule>
603+
541604

542-
<!-- And the rest should be ignored -->
605+
<!-- The next rule specify the credential type enum -->
606+
<rule>
607+
<match>
608+
<identifier name="git_credtype_t"/>
609+
</match>
610+
<wrapper type="default">
611+
</wrapper>
612+
</rule>
613+
614+
615+
<!-- And the rest should be ignored -->
543616
<rule>
544617
<match>
545618
</match>

library/generated_wrapper/c/include/ewg_libgit2_callback_c_glue_code.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,21 @@ void set_git_checkout_perfdata_cb_entry (void* a_class, void* a_feature);
103103
void call_git_checkout_perfdata_cb (void *a_function, git_checkout_perfdata const *perfdata, void *payload);
104104

105105

106+
#include <git2.h>
107+
108+
typedef int (*git_cred_acquire_cb_eiffel_feature) (void *a_class, git_cred **cred, char const *url, char const *username_from_url, unsigned int allowed_types, void *payload);
109+
110+
void* get_git_cred_acquire_cb_stub ();
111+
112+
struct git_cred_acquire_cb_entry_struct
113+
{
114+
void* a_class;
115+
git_cred_acquire_cb_eiffel_feature feature;
116+
};
117+
118+
void set_git_cred_acquire_cb_entry (void* a_class, void* a_feature);
119+
120+
int call_git_cred_acquire_cb (void *a_function, git_cred **cred, char const *url, char const *username_from_url, unsigned int allowed_types, void *payload);
121+
122+
106123
#endif

library/generated_wrapper/c/src/ewg_libgit2_callback_c_glue_code.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,29 @@ void call_git_checkout_perfdata_cb (void *a_function, git_checkout_perfdata cons
160160
((void (*) (git_checkout_perfdata const *perfdata, void *payload))a_function) (perfdata, payload);
161161
}
162162

163+
struct git_cred_acquire_cb_entry_struct git_cred_acquire_cb_entry = {NULL, NULL};
164+
165+
int git_cred_acquire_cb_stub (git_cred **cred, char const *url, char const *username_from_url, unsigned int allowed_types, void *payload)
166+
{
167+
if (git_cred_acquire_cb_entry.a_class != NULL && git_cred_acquire_cb_entry.feature != NULL)
168+
{
169+
return git_cred_acquire_cb_entry.feature (eif_access(git_cred_acquire_cb_entry.a_class), cred, url, username_from_url, allowed_types, payload);
170+
}
171+
}
172+
173+
void set_git_cred_acquire_cb_entry (void* a_class, void* a_feature)
174+
{
175+
git_cred_acquire_cb_entry.a_class = eif_adopt(a_class);
176+
git_cred_acquire_cb_entry.feature = (git_cred_acquire_cb_eiffel_feature) a_feature;
177+
}
178+
179+
void* get_git_cred_acquire_cb_stub ()
180+
{
181+
return (void*) git_cred_acquire_cb_stub;
182+
}
183+
184+
int call_git_cred_acquire_cb (void *a_function, git_cred **cred, char const *url, char const *username_from_url, unsigned int allowed_types, void *payload)
185+
{
186+
return ((int (*) (git_cred **cred, char const *url, char const *username_from_url, unsigned int allowed_types, void *payload))a_function) (cred, url, username_from_url, allowed_types, payload);
187+
}
188+

library/generated_wrapper/eiffel/ewg_libgit2_callback_c_glue_code_functions_api.e

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@ feature -- Access
140140
c_call_git_checkout_perfdata_cb (a_function, perfdata.item, payload)
141141
end
142142

143+
get_git_cred_acquire_cb_stub: POINTER
144+
external
145+
"C inline use <ewg_libgit2_callback_c_glue_code.h>"
146+
alias
147+
"[
148+
return get_git_cred_acquire_cb_stub ();
149+
]"
150+
end
151+
152+
set_git_cred_acquire_cb_entry (a_class: GIT_CRED_ACQUIRE_CB_DISPATCHER; a_feature: POINTER)
153+
do
154+
c_set_git_cred_acquire_cb_entry (a_class, a_feature)
155+
end
156+
157+
call_git_cred_acquire_cb (a_function: POINTER; cred: GIT_CRED_STRUCT_API; url: STRING; username_from_url: STRING; allowed_types: INTEGER; payload: POINTER): INTEGER
158+
local
159+
url_c_string: C_STRING
160+
username_from_url_c_string: C_STRING
161+
do
162+
create url_c_string.make (url)
163+
create username_from_url_c_string.make (username_from_url)
164+
Result := c_call_git_cred_acquire_cb (a_function, cred.item, url_c_string.item, username_from_url_c_string.item, allowed_types, payload)
165+
end
166+
143167
feature -- Externals
144168

145169
c_set_git_index_matched_path_cb_entry (a_class: GIT_INDEX_MATCHED_PATH_CB_DISPATCHER; a_feature: POINTER)
@@ -250,6 +274,24 @@ feature -- Externals
250274
]"
251275
end
252276

277+
c_set_git_cred_acquire_cb_entry (a_class: GIT_CRED_ACQUIRE_CB_DISPATCHER; a_feature: POINTER)
278+
external
279+
"C inline use <ewg_libgit2_callback_c_glue_code.h>"
280+
alias
281+
"[
282+
set_git_cred_acquire_cb_entry ((void*)$a_class, (void*)$a_feature);
283+
]"
284+
end
285+
286+
c_call_git_cred_acquire_cb (a_function: POINTER; cred: POINTER; url: POINTER; username_from_url: POINTER; allowed_types: INTEGER; payload: POINTER): INTEGER
287+
external
288+
"C inline use <ewg_libgit2_callback_c_glue_code.h>"
289+
alias
290+
"[
291+
return call_git_cred_acquire_cb ((void*)$a_function, (git_cred**)$cred, (char const*)$url, (char const*)$username_from_url, (unsigned int)$allowed_types, (void*)$payload);
292+
]"
293+
end
294+
253295
feature -- Externals Address
254296

255297
end

0 commit comments

Comments
 (0)