|
| 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