-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.e
More file actions
577 lines (500 loc) · 18.9 KB
/
Copy pathapplication.e
File metadata and controls
577 lines (500 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
note
description: "[
libgit2 "status" example - shows how to use the status APIs
]"
EIS: "name=git status", "src=https://github.com/libgit2/libgit2/blob/master/examples/status.c","protocol=uri"
class APPLICATION
inherit
COMMAND_LINE_PARSER
rename
make as make_command_line_parser
end
create
make
feature {NONE} --Initialization
make
local
status: GIT_STATUS_LIST_STRUCT_API
repo: GIT_REPOSITORY_STRUCT_API
ini: INTEGER
callback: GIT_SUBMODULE_CB_DISPATCHER
count: INTEGER
do
create repo.make
ini := {LIBGIT2_INITIALIZER_API}.git_libgit2_init
debug
print ("%N Intializing Libgit2")
end
create options.make
options.status_options.set_show ({GIT_STATUS_SHOW_T_ENUM_API}.GIT_STATUS_SHOW_INDEX_AND_WORKDIR)
options.status_options.set_flags ( {GIT_STATUS_OPT_T_ENUM_API}.git_status_opt_include_untracked |
{GIT_STATUS_OPT_T_ENUM_API}.git_status_opt_renames_head_to_index |
{GIT_STATUS_OPT_T_ENUM_API}.git_status_opt_sort_case_sensitively)
create status.make
make_command_line_parser
process_arguments
if {LIBGIT2_REPOSITORY}.git_repository_open (repo, (create {PATH}.make_from_string (options.repodir)).out) < 0 then
print ("%NCould not open repository")
{EXCEPTIONS}.die (1)
end
if {LIBGIT2_REPOSITORY}.git_repository_is_bare (repo) < 0 then
print ("%NCannot report status on bare repository " + {LIBGIT2_REPOSITORY}.git_repository_path (repo))
{EXCEPTIONS}.die (1)
end
-- Show status
-- * Run status on the repository
-- *
-- * We use `git_status_list_new()` to generate a list of status
-- * information which lets us iterate over it at our
-- * convenience and extract the data we want to show out of
-- * each entry.
-- *
-- * You can use `git_status_foreach()` or
-- * `git_status_foreach_ext()` if you'd prefer to execute a
-- * callback for each entry. The latter gives you more control
-- * about what results are presented.
-- */
if {GIT_STATUS}.git_status_list_new (status, repo, options.status_options) < 0 then
print ("%NCould not get status " + {LIBGIT2_REPOSITORY}.git_repository_path (repo))
{EXCEPTIONS}.die (1)
end
if options.show_branch then
show_branch (repo)
end
create callback.make
callback.register_callback_1(agent print_submodule)
if options.show_submodule then
-- to be completed.
if {GIT_REFERENCE}.git_submodule_foreach (repo, callback.c_dispatcher_1, $count ) < 0 then
print ("%N# Cannot iterate submodules" + options.repodir)
end
end
if options.format = {FORMAT_ENUM}.format_long then
print_long (status)
else
print_short (repo, status)
end
{GIT_STATUS}.git_status_list_free (status)
end
show_branch (a_repo: GIT_REPOSITORY_STRUCT_API)
-- If the user asked for the branch, let's show the short name of the branch
local
head: GIT_REFERENCE_STRUCT_API
error: INTEGER
branch: STRING
do
create head.make
error := {LIBGIT2_REPOSITORY}.git_repository_head(head, a_repo)
if error = {GIT_ERROR_CODE_ENUM_API}.GIT_EUNBORNBRANCH or error={GIT_ERROR_CODE_ENUM_API}.GIT_EUNBORNBRANCH then
branch := ""
elseif error >= 0 then
branch := {GIT_REFERENCE}.git_reference_shorthand (head)
else
print ("%Nfailed to get current branch")
{EXCEPTIONS}.die (1)
end
print ("%NOn branch " + branch)
{GIT_REFERENCE}.git_reference_free (head)
end
feature {NONE} -- Process Arguments
process_arguments
-- Process command line arguments
do
if match_long_option ("short") then
consume_option
options.set_format ({FORMAT_ENUM}.FORMAT_SHORT)
end
if match_long_option ("long") then
consume_option
options.set_format ({FORMAT_ENUM}.FORMAT_LONG)
end
if match_long_option ("porcelain") then
consume_option
options.set_format ({FORMAT_ENUM}.FORMAT_PORCELAIN)
end
if match_long_option ("branch") then
consume_option
options.set_show_branch (True)
end
if match_long_option ("z") then
consume_option
options.set_zterm (1)
if options.format = {FORMAT_ENUM}.FORMAT_DEFAULT then
options.set_format ({FORMAT_ENUM}.FORMAT_PORCELAIN)
end
end
if match_long_option ("ignored") then
consume_option
options.status_options.set_flags (options.status_options.flags | {GIT_STATUS_OPT_T_ENUM_API}.git_status_opt_include_ignored)
end
--untracked-files=no
if match_long_option ("uno") then
consume_option
options.status_options.set_flags (options.status_options.flags & ({GIT_STATUS_OPT_T_ENUM_API}.git_status_opt_include_untracked).bit_not)
end
--untracked-files=normal
if match_long_option ("unormal") then
consume_option
options.status_options.set_flags (options.status_options.flags | {GIT_STATUS_OPT_T_ENUM_API}.git_status_opt_include_untracked)
end
--untracked-files=all
if match_long_option ("uall") then
consume_option
options.status_options.set_flags (options.status_options.flags | {GIT_STATUS_OPT_T_ENUM_API}.git_status_opt_include_untracked | {GIT_STATUS_OPT_T_ENUM_API}.git_status_opt_recurse_untracked_dirs )
end
--ignore-submodules=all
if match_long_option ("ignore-submodules") then
consume_option
options.status_options.set_flags (options.status_options.flags | {GIT_STATUS_OPT_T_ENUM_API}.git_status_opt_exclude_submodules )
end
if match_long_option ("git-dir") then
if is_next_option_long_option and then has_next_option_value then
options.set_repodir (next_option_value.to_string_8)
consume_option
else
print("%N Missing command line parameter --git-dir=<dir>")
usage
{EXCEPTIONS}.die (1)
end
end
if match_long_option ("list-submodules") then
consume_option
options.set_show_submodule (True)
end
end
feature -- Print
print_long (a_status: GIT_STATUS_LIST_STRUCT_API)
local
maxi: INTEGER
changed_in_index: BOOLEAN
header: BOOLEAN
changed_in_workdir, rm_in_workdir: BOOLEAN
old_path, new_path: STRING
is_status: STRING
ws_status: STRING
continue: BOOLEAN
do
maxi := {GIT_STATUS}.git_status_list_entrycount (a_status)
-- Print index changes
across 1 |..| maxi as ic loop
create is_status.make_empty
continue := False
if attached {GIT_STATUS_ENTRY_STRUCT_API} {GIT_STATUS}.git_status_byindex (a_status, ic.item - 1) as s then
if s.status = {GIT_STATUS_T_ENUM_API}.Git_Status_current then
continue := True
end
if (s.status & {GIT_STATUS_T_ENUM_API}.git_status_wt_deleted) /= 0 and not continue then
rm_in_workdir := True
end
if (s.status & {GIT_STATUS_T_ENUM_API}.git_status_index_new) /= 0 and not continue then
is_status.append ("new file: ")
end
if (s.status & {GIT_STATUS_T_ENUM_API}.git_status_index_modified) /= 0 and not continue then
is_status.append ("modified: ")
end
if (s.status & {GIT_STATUS_T_ENUM_API}.git_status_index_deleted) /= 0 and not continue then
is_status.append ("deleted: ")
end
if (s.status & {GIT_STATUS_T_ENUM_API}.git_status_index_renamed) /= 0 and not continue then
is_status.append ("renamed: ")
end
if (s.status & {GIT_STATUS_T_ENUM_API}.git_status_index_typechange) /= 0 and not continue then
is_status.append ("typechange: ")
end
if is_status.is_empty then
continue := True
end
if not continue then
if not header then
print("%N# Changes to be committed:%N")
print("%N# (use %"git reset HEAD <file>...%" to unstage)%N")
print("%N#%N");
header := True
end
if attached s.head_to_index as l_head_to_index and then
attached l_head_to_index.old_file.path as l_old_file
then
old_path := l_old_file.string
else
old_path := Void
end
if attached s.head_to_index as l_head_to_index and then
attached l_head_to_index.new_file.path as l_new_file
then
new_path := l_new_file.string
else
old_path := Void
end
if old_path /= Void and then new_path /= Void and then old_path.is_case_insensitive_equal_general (new_path) then
print ("%N%T" + is_status + " " + old_path + " -> " + new_path )
else
if old_path /= Void and then old_path.is_empty then
print ("%N%T" + is_status + " " + if attached new_path then new_path else "" end)
else
print ("%N%T" + is_status + " " + if attached old_path then old_path else "" end)
end
end
end
end
end
if header then
changed_in_index := True
print ("#%N")
end
header := False
-- Print workdir changes to tracked files.
across 1 |..| maxi as ic loop
create ws_status.make_empty
continue := False
if attached {GIT_STATUS_ENTRY_STRUCT_API} {GIT_STATUS}.git_status_byindex (a_status, ic.item - 1) as s then
--
-- With `GIT_STATUS_OPT_INCLUDE_UNMODIFIED` (not used in this example)
-- `index_to_workdir` may not be `NULL` even if there are
-- no differences, in which case it will be a `GIT_DELTA_UNMODIFIED`.
--
if s.status ={GIT_STATUS_T_ENUM_API}.git_status_current or (attached s.index_to_workdir as l_index_to_workdir and then l_index_to_workdir.item = default_pointer) then
continue := True
end
-- Print out the output since we know the file has some changes
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_modified and not continue then
ws_status := "modified: "
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_deleted and not continue then
ws_status := "deleted: "
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_renamed and not continue then
ws_status := "renamed: "
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_index_typechange and not continue then
ws_status := "typechange: "
end
if ws_status.is_empty then
continue := True
end
if not continue then
if not header then
print("# Changes not staged for commit:%N");
if rm_in_workdir then
print ("# (use %"git add /rm <file>...%" to update what will be committed)%N")
else
print ("# (use %"git add <file>...%" to update what will be committed)%N")
end
print ("# (use %"git checkout -- <file>...%" to discard changes in working directory)%N")
print ("#%N");
header := True
end
if attached s.index_to_workdir as l_index_to_workdir and then
attached l_index_to_workdir.old_file.path as l_old_file
then
old_path := l_old_file.string
end
if attached s.index_to_workdir as l_index_to_workdir and then
attached l_index_to_workdir.new_file.path as l_new_file
then
new_path := l_new_file.string
end
if old_path /= Void and then new_path /= Void then
if not old_path.is_case_insensitive_equal_general (new_path) then
print ("%N%T" + ws_status + " " + old_path + " > " + new_path +"%N" )
else
print ("%N%T" + ws_status + " " + if attached old_path then old_path else "" end)
end
end
end
end
end
if header then
changed_in_workdir := True
print ("#%N")
end
-- Print untracked files.
header := False
across 1 |..| maxi as ic loop
if attached {GIT_STATUS_ENTRY_STRUCT_API} {GIT_STATUS}.git_status_byindex (a_status, ic.item - 1) as s then
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_new then
if not header then
print("# Untracked files:%N");
print("# (use %"git add <file>...%" to include in what will be committed)%N");
print("#%N");
header := True;
end
print("#%T" + if attached s.index_to_workdir as l_index_workdir and then attached l_index_workdir.old_file as l_old_file and then attached l_old_file.path as l_path then l_path.string else "" end + "%N")
end
end
end
header := False
-- Print ignored files.
across 1 |..| maxi as ic loop
if attached {GIT_STATUS_ENTRY_STRUCT_API} {GIT_STATUS}.git_status_byindex (a_status, ic.item - 1) as s then
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_ignored then
if not header then
print("# Ignored files:%N");
print("# (use %"git add <file>...%" to include in what will be committed)%N");
print("#%N");
header := True;
end
print("#%T" + if attached s.index_to_workdir as l_index_workdir and then attached l_index_workdir.old_file as l_old_file and then attached l_old_file.path as l_path then l_path.string else "" end)
end
end
end
if not changed_in_index and changed_in_workdir then
print("%Nno changes added to commit (use %"git add%" and/or %"git commit -a%")%N");
end
end
print_short (a_repo: GIT_REPOSITORY_STRUCT_API; a_status: GIT_STATUS_LIST_STRUCT_API)
-- This version of the output prefixes each path with two status columns and shows submodule status information.
local
maxi: INTEGER
is_status: STRING
ws_status: STRING
continue: BOOLEAN
a,b,c, extra: STRING
sm_status: INTEGER
do
maxi := {GIT_STATUS}.git_status_list_entrycount (a_status)
across 1 |..| maxi as ic loop
continue := False
if attached {GIT_STATUS_ENTRY_STRUCT_API} {GIT_STATUS}.git_status_byindex (a_status, ic.item - 1) as s then
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_current then
continue := True
end
a := Void
b := Void
c := Void
extra := ""
is_status := ""
ws_status := ""
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_index_new and not continue then
is_status := "A"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_index_modified and not continue then
is_status := "M"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_index_deleted and not continue then
is_status := "D"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_index_renamed and not continue then
is_status := "R"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_typechange and not continue then
is_status := "T"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_new and not continue then
if is_status.is_empty then
is_status := "?"
end
ws_status := "?"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_modified and not continue then
ws_status := "M"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_deleted and not continue then
ws_status := "D"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_renamed and not continue then
ws_status := "R"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_typechange and not continue then
ws_status := "T"
end
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_ignored and not continue then
ws_status := "!"
is_status := "!"
end
if is_status.same_string ("?") and ws_status.same_string ("?") and then not continue then
continue := True
end
-- A commit in a tree is how submodules are stored, so
-- let's go take a look at its status
if not continue then
if attached s.index_to_workdir as l_index_to_dir and then
attached l_index_to_dir.new_file as l_file and then
attached l_file.path as l_path and then
l_file.mode = {GIT_FILEMODE_T_ENUM_API}.git_filemode_commit
then
sm_status := 0
if {GIT_REFERENCE}.git_submodule_status ($sm_status, a_repo, l_path.string ,{GIT_SUBMODULE_IGNORE_T_ENUM_API}.GIT_SUBMODULE_IGNORE_UNSPECIFIED) = 0 then
if sm_status & {GIT_SUBMODULE_STATUS_T_ENUM_API}.GIT_SUBMODULE_STATUS_WD_MODIFIED /= 0 then
extra := " (new commits)"
elseif sm_status & {GIT_SUBMODULE_STATUS_T_ENUM_API}.GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED /= 0 then
extra := " (modified commits)"
elseif sm_status & {GIT_SUBMODULE_STATUS_T_ENUM_API}.GIT_SUBMODULE_STATUS_WD_WD_MODIFIED /= 0 then
extra := " (modified content)"
elseif sm_status & {GIT_SUBMODULE_STATUS_T_ENUM_API}.GIT_SUBMODULE_STATUS_WD_UNTRACKED /= 0 then
extra := " (untracked content)"
end
end
end
-- Now that we have all the information, format the output.
if attached s.head_to_index as l_head_to_index then
a := if attached l_head_to_index.old_file.path as l_old_file then l_old_file.string else Void end
b := if attached l_head_to_index.new_file.path as l_new_file then l_new_file.string else Void end
end
if attached s.index_to_workdir as l_index_to_workdir and not continue then
if a = Void then
a := if attached l_index_to_workdir.old_file.path as l_old_file then l_old_file.string else Void end
end
if b = Void then
b := if attached l_index_to_workdir.old_file.path as l_old_file then l_old_file.string else Void end
end
c := if attached l_index_to_workdir.new_file.path as l_new_file then l_new_file.string else Void end
end
if is_status.same_string ("R") then
if ws_status.same_string ("R") then
print ("%N " + is_status + " " + ws_status + " " + if attached a then a else "" end + " " +
if attached b then b else "" end + " " + if attached c then c else "" end + " " + extra)
else
print ("%N " + is_status + " " + ws_status + " " + if attached a then a else "" end + " " +
if attached b then b else "" end + " " + extra)
end
else
if ws_status.same_string ("R") then
print ("%N " + is_status + " " + ws_status + " " + if attached a then a else "" end + " " +
if attached c then c else "" end + " " + extra)
else
print ("%N " + is_status + " " + ws_status + " " + if attached a then a else "" end + " " + extra)
end
end
end
end
end
across 1 |..| maxi as ic loop
if attached {GIT_STATUS_ENTRY_STRUCT_API} {GIT_STATUS}.git_status_byindex (a_status, ic.item - 1) as s then
if s.status = {GIT_STATUS_T_ENUM_API}.git_status_wt_new then
print ("%N?? " + if attached s.index_to_workdir as l_index_to_workdir and then attached l_index_to_workdir.old_file.path as l_old_file and then attached l_old_file.string as l_path then l_path else "" end )
end
end
end
print ("%N")
end
print_submodule (sm: POINTER; name: POINTER; payload: POINTER): INTEGER
local
count: INTEGER
do
count := payload.to_integer_32
if count = 0 then
print("%N# Submodules");
end
count := count + 1
payload.set_item ($count)
print ("# -submodule " + (create {C_STRING}.make_by_pointer ({GIT_REFERENCE}.c_git_submodule_name (sm))).string + " at " + (create {C_STRING}.make_by_pointer ({GIT_REFERENCE}.c_git_submodule_path (sm))).string )
Result := 0
end
feature -- Usage
usage
local
str: STRING
do
str := "[
git_status [--short] [--long] [--porcelain]
[--branch] [--z] [--ignored] [--uno] [--unormal] [--uall]
[--ignore-submodules] [--git-dir=<dir>]
[--list-submodules]
]"
print("%N")
print (str)
print("%N")
end
feature -- Options
options: OPTIONS
end