Skip to content

Commit 6392bab

Browse files
authored
Merge pull request #1 from jvelilla/wrap_libgit2_dev
Wrap libgit2 dev
2 parents 2a52a67 + 8f59843 commit 6392bab

26 files changed

+3771
-4
lines changed

Readme.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ allowing you to write native speed custom Git applications in any language which
1010
* [WrapC](https://github.com/eiffel-wrap-c/WrapC) tool.
1111
* [Libgit2 v0.28.3](https://github.com/libgit2/libgit2/releases).
1212

13-
== Status ==
13+
### Status
1414
The binding is work in progress.
1515
Tested on Linux and Windows 64 bits.
1616

1717
## Examples
1818

19-
* Git Init: `shows how to initialize a new repo`
20-
* Git Status: `shows how to use the status APIs`
19+
* [Git Init](./examples/init): `shows how to initialize a new repo`
20+
* [Git Status](./examples/status): `shows how to use the status APIs`
21+
* [Git Add](./examples/add) `shows how to modify the index`
22+
* [Git Checkout](./examples/checkout) `shows how to perform checkouts`.
23+
* [Git Describe](./examples/describe) `shows how to describe commits`.
24+
* [Git ls-files](./examples/ls_files) `shows how to view all files currently in the index.`
25+
* [Git push](./examples/push) `shows how to git push <remote> <branch>`.
2126

2227
[Guide to linking libgit2](https://libgit2.org/docs/guides/build-and-link/) on various platforms
2328

@@ -28,6 +33,7 @@ On Linux to install version 0.28.3 you will need to do the following.
2833
$ cmake ..
2934
$ sudo cmake --build . --target install
3035

36+
3137
Optionally you can use [vckpg](https://github.com/Microsoft/vcpkg), a C++ Library Manager for Windows, Linux, and MacOS.
3238

3339
Windows example
@@ -48,3 +54,4 @@ Linux example
4854

4955

5056

57+

examples/push/application.e

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
note
2+
description: "[
3+
libgit2 "push" example - shows how to git push <remote> <branch>
4+
]"
5+
6+
class APPLICATION
7+
8+
inherit
9+
10+
COMMAND_LINE_PARSER
11+
rename
12+
make as make_command_line_parser
13+
end
14+
15+
create
16+
make
17+
18+
feature {NONE} --Initialization
19+
20+
make
21+
22+
do
23+
create git_repository
24+
path := "."
25+
remote := ""
26+
branch := ""
27+
28+
make_command_line_parser
29+
process_arguments
30+
push_repository
31+
end
32+
33+
34+
feature -- Intiialize Repository
35+
36+
push_repository
37+
local
38+
ini: INTEGER
39+
repo: GIT_REPOSITORY_STRUCT_API
40+
iniopts: GIT_REPOSITORY_INIT_OPTIONS_STRUCT_API
41+
l_remote: GIT_REMOTE_STRUCT_API
42+
git_remote: GIT_REMOTE
43+
refspec: STRING
44+
l_options: GIT_PUSH_OPTIONS_STRUCT_API
45+
a_array: GIT_STRARRAY_STRUCT_API
46+
callbacks: GIT_REMOTE_CALLBACKS_STRUCT_API
47+
callback_dispatcher: GIT_CRED_ACQUIRE_CB_DISPATCHER
48+
49+
do
50+
ini := {LIBGIT2_INITIALIZER_API}.git_libgit2_init
51+
print ("%N Intializing Libgit2")
52+
create repo.make
53+
54+
if git_repository.git_repository_open (repo, (create {PATH}.make_from_string (path)).out) < 0 then
55+
print ("%NCould not open repository")
56+
{EXCEPTIONS}.die (1)
57+
end
58+
59+
60+
-- get the remote
61+
create l_remote.make
62+
63+
create git_remote
64+
if git_remote.git_remote_lookup (l_remote, repo, remote) < 0 then
65+
print ("%NCould not get remote repository " + remote)
66+
{EXCEPTIONS}.die (1)
67+
end
68+
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+
78+
-- connect to remote
79+
if git_remote.git_remote_connect (l_remote, {GIT_DIRECTION_ENUM_API}.git_direction_push, callbacks, Void, Void) < 0 then
80+
print ("%NCould not connect to remote repository " + remote)
81+
{EXCEPTIONS}.die (1)
82+
end
83+
84+
85+
-- add a push refspec
86+
create refspec.make_from_string ("refs/heads/")
87+
refspec.append (branch)
88+
89+
if git_remote.git_remote_add_push (repo, remote, refspec + ":" + refspec) < 0 then
90+
print ("%NCould not add push ")
91+
{EXCEPTIONS}.die (1)
92+
end
93+
94+
-- configure options
95+
create l_options.make
96+
if git_remote.git_push_init_options (l_options, 1) < 0 then
97+
print ("%NCould not configure options ")
98+
{EXCEPTIONS}.die (1)
99+
end
100+
101+
create a_array.make
102+
init_array (a_array, {ARRAY [STRING]}<<refspec + ":" + refspec>>)
103+
104+
-- do the push
105+
if git_remote.git_remote_upload (l_remote, a_array, l_options) < 0 then
106+
print ("%NCould not do push ")
107+
{EXCEPTIONS}.die (1)
108+
end
109+
110+
git_repository.git_repository_free (repo)
111+
git_remote.git_remote_free (l_remote)
112+
113+
end
114+
115+
init_array (a_array: GIT_STRARRAY_STRUCT_API; l_array: ARRAY [STRING])
116+
local
117+
mp: MANAGED_POINTER
118+
do
119+
create mp.make (l_array.count * {PLATFORM}.pointer_bytes)
120+
across l_array as ic loop
121+
mp.put_pointer ((create {C_STRING}.make (ic.item)).item, (ic.cursor_index - 1) * {PLATFORM}.pointer_bytes )
122+
end
123+
a_array.set_count (l_array.count)
124+
a_array.set_strings (mp.item)
125+
end
126+
127+
128+
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
184+
185+
process_arguments
186+
-- Process command line arguments
187+
local
188+
shared_value: STRING
189+
do
190+
if match_long_option ("git-dir") then
191+
if is_next_option_long_option and then has_next_option_value then
192+
create path.make_from_string (next_option_value)
193+
consume_option
194+
else
195+
print("%N Missing command line parameter --git-dir=<dir>")
196+
usage
197+
{EXCEPTIONS}.die (1)
198+
end
199+
end
200+
if has_next_option and then not is_next_option_long_option then
201+
create remote.make_from_string (next_option)
202+
consume_option
203+
else
204+
print("%N Missing command line parameter <remote>%N")
205+
usage
206+
{EXCEPTIONS}.die (1)
207+
end
208+
if has_next_option and then not is_next_option_long_option then
209+
create branch.make_from_string (next_option)
210+
consume_option
211+
else
212+
print("%N Missing command line parameter <branch>%N")
213+
usage
214+
{EXCEPTIONS}.die (1)
215+
end
216+
end
217+
218+
usage
219+
local
220+
str: STRING
221+
do
222+
str := "[
223+
git_push [--git-dir=<directory>]
224+
<remote>
225+
<branch>
226+
]"
227+
228+
print("%N")
229+
print (str)
230+
end
231+
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+
279+
feature -- Options
280+
281+
path: STRING
282+
remote: STRING
283+
branch: STRING
284+
git_repository: LIBGIT2_REPOSITORY
285+
286+
end

0 commit comments

Comments
 (0)