Skip to content

Commit 3fc33d1

Browse files
authored
Merge pull request #2 from jvelilla/wrap_libgit2_dev
Wrap libgit2 dev
2 parents 6392bab + 79ef186 commit 3fc33d1

File tree

7 files changed

+554
-5
lines changed

7 files changed

+554
-5
lines changed

Readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ Tested on Linux and Windows 64 bits.
1616

1717
## Examples
1818

19-
* [Git Init](./examples/init): `shows how to initialize a new repo`
20-
* [Git Status](./examples/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`
2121
* [Git Add](./examples/add) `shows how to modify the index`
2222
* [Git Checkout](./examples/checkout) `shows how to perform checkouts`.
2323
* [Git Describe](./examples/describe) `shows how to describe commits`.
2424
* [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>`.
25+
* [Git push](./examples/push) `shows how to git push <remote> <branch>`
26+
* [Git ls_remote](./examples/ls_remote) `shows how to list remote references.`
2627

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

examples/ls_remote/application.e

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
note
2+
description: "[
3+
libgit2 "ls_remote" example - shows how to list remote references
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+
27+
make_command_line_parser
28+
process_arguments
29+
show_remote
30+
end
31+
32+
33+
feature -- Intiialize Repository
34+
35+
show_remote
36+
local
37+
ini: INTEGER
38+
repo: GIT_REPOSITORY_STRUCT_API
39+
iniopts: GIT_REPOSITORY_INIT_OPTIONS_STRUCT_API
40+
l_remote: GIT_REMOTE_STRUCT_API
41+
git_remote: GIT_REMOTE
42+
refspec: STRING
43+
l_options: GIT_PUSH_OPTIONS_STRUCT_API
44+
a_array: GIT_STRARRAY_STRUCT_API
45+
callbacks: GIT_REMOTE_CALLBACKS_STRUCT_API
46+
callback_dispatcher: GIT_CRED_ACQUIRE_CB_DISPATCHER
47+
refs: ARRAYED_LIST [GIT_REMOTE_HEAD_STRUCT_API]
48+
ref_len: INTEGER
49+
git_oid: GIT_OID
50+
oid: STRING
51+
52+
do
53+
ini := {LIBGIT2_INITIALIZER_API}.git_libgit2_init
54+
print ("%NIntializing Libgit2")
55+
create repo.make
56+
57+
if git_repository.git_repository_open (repo, (create {PATH}.make_from_string (path)).out) < 0 then
58+
print ("%NCould not open repository")
59+
{EXCEPTIONS}.die (1)
60+
end
61+
62+
-- get the remote
63+
create l_remote.make
64+
65+
create git_remote
66+
if git_remote.git_remote_lookup (l_remote, repo, remote) < 0 then
67+
if git_remote.git_remote_create_anonymous (l_remote, repo, remote) < 0 then
68+
print ("%NCould not get remote repository " + remote)
69+
{EXCEPTIONS}.die (1)
70+
end
71+
end
72+
73+
create callbacks.make
74+
if git_remote.git_remote_init_callbacks (callbacks, 1) < 0 then
75+
print ("%NCould not intialize callback ")
76+
{EXCEPTIONS}.die (1)
77+
end
78+
79+
create callback_dispatcher.make (agent cred_acquire_cb )
80+
callbacks.set_credentials (callback_dispatcher.c_dispatcher)
81+
82+
-- connect to remote
83+
if git_remote.git_remote_connect (l_remote, {GIT_DIRECTION_ENUM_API}.git_direction_fetch, callbacks, Void, Void) < 0 then
84+
print ("%NCould not connect to remote repository " + remote)
85+
{EXCEPTIONS}.die (1)
86+
end
87+
88+
create refs.make (1)
89+
-- Get the list of references on the remote and print out
90+
-- their name next to what they point to.
91+
92+
if git_remote.git_remote_ls (refs, l_remote) < 0 then
93+
print ("%NCould not get the remote repository's reference advertisement list")
94+
{EXCEPTIONS}.die (1)
95+
end
96+
97+
create git_oid
98+
from
99+
refs.start
100+
until
101+
refs.after
102+
loop
103+
create oid.make ({LIBGIT2_CONSTANTS}.GIT_OID_HEXSZ + 1)
104+
if attached refs.item_for_iteration.oid as l_oid then
105+
git_oid.git_oid_fmt (oid, l_oid)
106+
if attached refs.item_for_iteration.name as l_name then
107+
print("%N" + oid.substring (1, {LIBGIT2_CONSTANTS}.GIT_OID_HEXSZ) +"%T" + l_name )
108+
else
109+
print("%N" + oid +"%T")
110+
end
111+
end
112+
refs.forth
113+
114+
end
115+
git_repository.git_repository_free (repo)
116+
git_remote.git_remote_free (l_remote)
117+
ini := {LIBGIT2_INITIALIZER_API}.git_libgit2_shutdown
118+
io.read_line
119+
end
120+
121+
init_array (a_array: GIT_STRARRAY_STRUCT_API; l_array: ARRAY [STRING])
122+
local
123+
mp: MANAGED_POINTER
124+
do
125+
create mp.make (l_array.count * {PLATFORM}.pointer_bytes)
126+
across l_array as ic loop
127+
mp.put_pointer ((create {C_STRING}.make (ic.item)).item, (ic.cursor_index - 1) * {PLATFORM}.pointer_bytes )
128+
end
129+
a_array.set_count (l_array.count)
130+
a_array.set_strings (mp.item)
131+
end
132+
133+
cred_acquire_cb (a_cred: POINTER; a_url: POINTER; a_username_from_url: POINTER; a_allowed_types: INTEGER; a_payload: POINTER): INTEGER
134+
local
135+
l_user_name: STRING
136+
exit: BOOLEAN
137+
cred: GIT_CRED_STRUCT_API
138+
git_cred: GIT_CREDENTIALS_API
139+
l_password: STRING
140+
l_privkey: STRING
141+
l_pubkey: STRING
142+
do
143+
144+
if a_username_from_url /= default_pointer then
145+
l_user_name := (create {C_STRING}.make_by_pointer (a_username_from_url)).string
146+
exit := l_user_name.is_empty
147+
else
148+
print ("%NUsername:")
149+
io.read_line
150+
l_user_name := io.last_string.twin
151+
exit := l_user_name.is_empty
152+
end
153+
154+
if not exit and then a_allowed_types & {GIT_CREDTYPE_T_ENUM_API}.GIT_CREDTYPE_SSH_KEY > 0 then
155+
print ("%NSSH key:")
156+
io.read_line
157+
l_privkey := io.last_string.twin
158+
print ("%NPassword:")
159+
l_password := read_password
160+
if l_password.is_empty or l_privkey.is_empty then
161+
exit := True
162+
end
163+
create l_pubkey.make_from_string (l_password)
164+
l_pubkey.append_string (".pub")
165+
create git_cred
166+
create cred.make_by_pointer (a_cred)
167+
Result := git_cred.git_cred_ssh_key_new(cred, l_user_name, l_pubkey, l_privkey, l_password)
168+
elseif not exit and then a_allowed_types & {GIT_CREDTYPE_T_ENUM_API}.GIT_CREDTYPE_USERPASS_PLAINTEXT > 0 then
169+
print ("%NPassword:")
170+
l_password := read_password
171+
exit := l_password.is_empty
172+
if not exit then
173+
create git_cred
174+
create cred.make_by_pointer (a_cred)
175+
Result := git_cred.git_cred_userpass_plaintext_new(cred, l_user_name, l_password)
176+
end
177+
else
178+
if not exit then
179+
create git_cred
180+
create cred.make_by_pointer (a_cred)
181+
Result := git_cred.git_cred_username_new (cred, l_user_name)
182+
end
183+
end
184+
185+
end
186+
187+
feature {NONE} -- Process Arguments
188+
189+
process_arguments
190+
-- Process command line arguments
191+
local
192+
shared_value: STRING
193+
do
194+
if match_long_option ("git-dir") then
195+
if is_next_option_long_option and then has_next_option_value then
196+
create path.make_from_string (next_option_value)
197+
consume_option
198+
else
199+
print("%N Missing command line parameter --git-dir=<dir>")
200+
usage
201+
{EXCEPTIONS}.die (1)
202+
end
203+
end
204+
if has_next_option and then not is_next_option_long_option then
205+
create remote.make_from_string (next_option)
206+
consume_option
207+
else
208+
print("%N Missing command line parameter <remote>%N")
209+
usage
210+
{EXCEPTIONS}.die (1)
211+
end
212+
end
213+
214+
usage
215+
local
216+
str: STRING
217+
do
218+
str := "[
219+
lxs [--git-dir=<directory>]
220+
<remote>
221+
]"
222+
223+
print("%N")
224+
print (str)
225+
end
226+
227+
read_password: STRING
228+
local
229+
l_ptr: POINTER
230+
do
231+
l_ptr := c_read_password
232+
if l_ptr /= default_pointer then
233+
Result := (create {C_STRING}.make_by_pointer (l_ptr)).string
234+
else
235+
Result := ""
236+
end
237+
end
238+
239+
c_read_password: POINTER
240+
external "C inline"
241+
alias
242+
"[
243+
#define ENTER 13
244+
#define TAB 9
245+
#define BKSP 8
246+
247+
char* pwd;
248+
249+
int i = 0;
250+
char ch;
251+
252+
while(1){
253+
ch = getch(); //get key
254+
255+
if(ch == ENTER || ch == TAB){
256+
pwd[i] = '\0';
257+
break;
258+
}else if(ch == BKSP){
259+
if(i > 0){
260+
i--;
261+
printf("\b \b"); //for backspace
262+
}
263+
}else{
264+
pwd[i++] = ch;
265+
printf("* \b"); //to replace password character with *
266+
}
267+
}//while ends here
268+
269+
return pwd;
270+
]"
271+
end
272+
273+
feature -- Options
274+
275+
path: STRING
276+
remote: STRING
277+
git_repository: LIBGIT2_REPOSITORY
278+
279+
end

0 commit comments

Comments
 (0)