Skip to content

Commit 45a06e9

Browse files
committed
Initial Import wrap_libgit2 library
0 parents  commit 45a06e9

File tree

246 files changed

+42890
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

246 files changed

+42890
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* linguist-vendored
2+
*.e linguist-vendored=false

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
EIFGENs
2+
*.swp
3+
*.rc
4+

Readme.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Wrap_libgit2
2+
`Wrap_libgit2` is an Eiffel binding of [libgit2](https://libgit2.org/) C library
3+
using [WrapC](https://github.com/eiffel-wrap-c/WrapC) tool.
4+
5+
`libgit2` is a portable, pure C implementation of the Git core methods provided as a re-entrant linkable library with a solid API,
6+
allowing you to write native speed custom Git applications in any language which supports C bindings.
7+
8+
## Requirements
9+
10+
* [WrapC](https://github.com/eiffel-wrap-c/WrapC) tool.
11+
* [Libgit2 v0.28.3](https://github.com/libgit2/libgit2/releases).
12+
13+
== Status ==
14+
The binding is work in progress.
15+
Tested on Linux and Windows 64 bits.
16+
17+
## Examples
18+
19+
* Git Init: `shows how to initialize a new repo`
20+
* Git Status: `shows how to use the status APIs`
21+
22+
[Guide to linking libgit2](https://libgit2.org/docs/guides/build-and-link/) on various platforms
23+
24+
On Linux to install version 0.28.3 you will need to do the following.
25+
26+
$ mkdir build
27+
$ cd build
28+
$ cmake ..
29+
$ sudo cmake --build . --target install
30+
31+
Optionally you can use [vckpg](https://github.com/Microsoft/vcpkg), a C++ Library Manager for Windows, Linux, and MacOS.
32+
33+
Windows example
34+
```
35+
vcpkg install libgit2:x64-windows
36+
```
37+
or
38+
Linux example
39+
```
40+
./vcpkg install libgit2:x64-linux
41+
```
42+
43+
44+
45+
46+
47+
48+
49+
50+

examples/add/add.ecf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-21-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-21-0 http://www.eiffel.com/developers/xml/configuration-1-21-0.xsd" name="add">
3+
<target name="add">
4+
<root class="APPLICATION" feature="make"/>
5+
<file_rule>
6+
<exclude>/CVS$</exclude>
7+
<exclude>/EIFGENs$</exclude>
8+
<exclude>/\.git$</exclude>
9+
<exclude>/\.svn$</exclude>
10+
</file_rule>
11+
<option warning="warning" manifest_array_type="mismatch_warning">
12+
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
13+
</option>
14+
<setting name="console_application" value="true"/>
15+
<setting name="dead_code_removal" value="feature"/>
16+
<library name="base" location="$ISE_LIBRARY\library\base\base.ecf"/>
17+
<library name="libgit2" location="..\..\library\libgit2.ecf" readonly="false"/>
18+
<cluster name="add" location=".\" recursive="true"/>
19+
</target>
20+
</system>

examples/add/application.e

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
note
2+
description: "[
3+
libgit2 "add" example - shows how to modify the index
4+
]"
5+
EIS: "name=git add", "src=https://github.com/libgit2/libgit2/blob/master/examples/add.c","protocol=uri"
6+
7+
class APPLICATION
8+
9+
inherit
10+
11+
COMMAND_LINE_PARSER
12+
rename
13+
make as make_command_line_parser
14+
end
15+
16+
create
17+
make
18+
19+
feature {NONE} --Initialization
20+
21+
make
22+
23+
do
24+
create git_repository
25+
create path.make_from_string (".")
26+
create files.make (1)
27+
28+
make_command_line_parser
29+
process_arguments
30+
add
31+
end
32+
33+
34+
feature -- Repository
35+
36+
add
37+
local
38+
ini: INTEGER
39+
index: GIT_INDEX_STRUCT_API
40+
array: GIT_STRARRAY_STRUCT_API
41+
count: INTEGER
42+
payload: PAYLOAD
43+
matched_cb: GIT_INDEX_MATCHED_PATH_CB_DISPATCHER
44+
repo: GIT_REPOSITORY_STRUCT_API
45+
git_index: GIT_INDEX_API
46+
callback: POINTER
47+
do
48+
ini := {LIBGIT2_INITIALIZER_API}.git_libgit2_init
49+
print ("%NIntializing Libgit2%N")
50+
51+
create index.make
52+
create array.make
53+
create payload
54+
create repo.make
55+
56+
if git_repository.git_repository_open (repo, (create {PATH}.make_from_string (path)).out) < 0 then
57+
print ("%NCould not open repository")
58+
{EXCEPTIONS}.die (1)
59+
end
60+
61+
if git_repository.git_repository_index (index, repo) < 0 then
62+
print ("%NCould not open repository index")
63+
{EXCEPTIONS}.die (1)
64+
end
65+
66+
init_array (array)
67+
-- git_array_to_eiffel_array (array)
68+
69+
-- Setup a callback if the requested options need it.
70+
if ((options & VERBOSE) /= 0 ) or ((options & SKIP) /= 0) then
71+
create matched_cb.make (agent print_matched_cb)
72+
end
73+
74+
-- Perform the requested action with the index and files.
75+
payload.set_options (options)
76+
payload.set_repository (repo)
77+
78+
callback := if attached matched_cb as l_matched_cb then l_matched_cb.c_dispatcher else default_pointer end
79+
80+
create git_index
81+
if (options & UPDATE) /= 0 then
82+
ini := git_index.git_index_update_all (index, array, callback, serialize (payload))
83+
else
84+
ini := git_index.git_index_add_all (index, array, 0, callback, serialize (payload))
85+
end
86+
87+
-- Clenup memory
88+
ini := git_index.git_index_write (index)
89+
git_index.git_index_free (index)
90+
end
91+
92+
print_matched_cb (a_path: POINTER; a_matched_pathspec: POINTER; a_payload: POINTER): INTEGER
93+
-- This callback is called for each file under consideration by
94+
-- git_index_(update|add)_all above.
95+
-- It makes uses of the callback's ability to abort the action.
96+
local
97+
l_payload: PAYLOAD
98+
ret: INTEGER
99+
status: INTEGER
100+
git_status: GIT_STATUS
101+
repo: GIT_REPOSITORY_STRUCT_API
102+
103+
do
104+
create git_status
105+
l_payload := deserialize (a_payload)
106+
if attached l_payload then
107+
repo := if attached l_payload.repo as l_repo then l_repo else create {GIT_REPOSITORY_STRUCT_API}.make end
108+
else
109+
create repo.make
110+
end
111+
-- Get the file status
112+
if git_status.git_status_file ($status, repo, (create {C_STRING}.make_by_pointer (a_path)).string) < 0 then
113+
Result := -1
114+
end
115+
116+
if ((( status & {GIT_STATUS_T_ENUM_API}.GIT_STATUS_WT_MODIFIED )/= 0 ) or ( status & ({GIT_STATUS_T_ENUM_API}.GIT_STATUS_WT_NEW) /= 0 )) and (Result /= -1) then
117+
print ("add " + (create {C_STRING}.make_by_pointer (a_path)).string )
118+
Result := 0
119+
else
120+
Result := 1
121+
end
122+
123+
if attached l_payload and then (l_payload.options & SKIP) /= 0 then
124+
Result := 1
125+
end
126+
127+
end
128+
129+
130+
feature {NONE} -- Process Arguments
131+
132+
133+
process_arguments
134+
-- Process command line arguments
135+
local
136+
shared_value: STRING
137+
do
138+
if match_long_option ("git-dir") then
139+
if is_next_option_long_option and then has_next_option_value then
140+
create path.make_from_string (next_option_value)
141+
consume_option
142+
else
143+
print("%N Missing command line parameter --git-dir=<dir>")
144+
usage
145+
{EXCEPTIONS}.die (1)
146+
end
147+
end
148+
149+
if match_long_option ("dry-run") then
150+
options := options | SKIP
151+
consume_option
152+
end
153+
154+
if match_long_option ("verbose") then
155+
options := options | VERBOSE
156+
consume_option
157+
end
158+
159+
if match_long_option ("update") then
160+
options := options | UPDATE
161+
consume_option
162+
end
163+
164+
from
165+
if has_next_option and then not is_next_option_long_option then
166+
files.force (next_option)
167+
consume_option
168+
else
169+
print("%N Missing command line parameter <file>%N")
170+
usage
171+
{EXCEPTIONS}.die (1)
172+
end
173+
until
174+
not has_next_option
175+
loop
176+
if has_next_option and then not is_next_option_long_option then
177+
files.force (next_option)
178+
consume_option
179+
else
180+
print ("%NUnexpected parameter%N")
181+
usage
182+
{EXCEPTIONS}.die (1)
183+
end
184+
end
185+
end
186+
187+
usage
188+
local
189+
str: STRING
190+
do
191+
str := "[
192+
git_add [--git-dir] [options] file [file]
193+
[--git-dir]: use the following git repository.
194+
[--dry-run]: dry-run
195+
[--verbose]: be verbose
196+
[--update]: update tracked files
197+
<files>
198+
]"
199+
200+
print("%N")
201+
print (str)
202+
end
203+
204+
init_array (a_array: GIT_STRARRAY_STRUCT_API)
205+
local
206+
l_array: ARRAY [STRING]
207+
mp: MANAGED_POINTER
208+
209+
do
210+
l_array := files.to_array
211+
212+
create mp.make (l_array.count * {PLATFORM}.pointer_bytes)
213+
across l_array as ic loop
214+
mp.put_pointer ((create {C_STRING}.make (ic.item)).item, (ic.cursor_index - 1) * {PLATFORM}.pointer_bytes )
215+
end
216+
217+
a_array.set_count (l_array.count)
218+
a_array.set_strings (mp.item)
219+
end
220+
221+
222+
git_array_to_eiffel_array (a_array: GIT_STRARRAY_STRUCT_API)
223+
local
224+
mp: MANAGED_POINTER
225+
l_arr: ARRAY [STRING]
226+
i: INTEGER
227+
do
228+
create mp.make_from_pointer (a_array.strings, a_array.count * {PLATFORM}.pointer_bytes)
229+
create l_arr.make_filled ("", 1, a_array.count)
230+
from
231+
232+
until
233+
i = a_array.count
234+
loop
235+
l_arr.put ((create {C_STRING}.make_by_pointer (mp.read_pointer (i*{PLATFORM}.pointer_bytes))).string, i + 1)
236+
i := i + 1
237+
end
238+
239+
end
240+
241+
deserialize (a_pointer: POINTER): PAYLOAD
242+
local
243+
mp: MANAGED_POINTER
244+
do
245+
create mp.make_from_pointer (a_pointer, {PLATFORM}.integer_32_bytes + {PLATFORM}.pointer_bytes)
246+
create Result
247+
Result.set_options (mp.read_integer_32 (0))
248+
Result.set_repository (create {GIT_REPOSITORY_STRUCT_API}.make_by_pointer (mp.read_pointer ({PLATFORM}.integer_32_bytes)))
249+
end
250+
251+
serialize (a_payload: PAYLOAD): POINTER
252+
local
253+
l_ptr: MANAGED_POINTER
254+
do
255+
create l_ptr.make ({PLATFORM}.integer_32_bytes + {PLATFORM}.pointer_bytes)
256+
l_ptr.put_integer_32 (a_payload.options, 0)
257+
l_ptr.put_pointer (a_payload.pointer, {PLATFORM}.integer_32_bytes)
258+
Result := l_ptr.item
259+
end
260+
261+
feature -- Options
262+
263+
git_repository: LIBGIT2_REPOSITORY
264+
path: STRING
265+
files: ARRAYED_LIST [STRING]
266+
options: INTEGER
267+
268+
SKIP: INTEGER = 1
269+
VERBOSE: INTEGER = 2
270+
UPDATE: INTEGER = 4
271+
272+
end

0 commit comments

Comments
 (0)