-
Notifications
You must be signed in to change notification settings - Fork 82
/
git.lua
3134 lines (2935 loc) · 111 KB
/
git.lua
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local path_module = require('path')
local git = require('gitutil')
local matchers = require('matchers')
local w = require('tables').wrap
local clink_version = require('clink_version')
local color = require('color')
require('arghelper')
local parser = function (...)
local p = clink.arg.new_parser(...)
p._deprecated = nil
return p
end
-- luacheck: globals matchicons
local argexpected = "Argument expected: "
local argoptional = "Optional argument: "
local function hintpfx(optional)
return optional and argoptional or argexpected
end
local function inc_num_args(user_data, word_index)
user_data.num_args = (user_data.num_args or 0) + 1
user_data.word_index = word_index
end
local function is_optional(user_data, word_index)
local u_num_args = user_data.num_args or 0
local u_word_index = user_data.word_index
return (u_num_args > 1) or (u_word_index and u_word_index < word_index)
end
if clink_version.supports_color_settings then
settings.add('color.git.star', 'bright green', 'Color for preferred branch completions')
end
local file_matches = clink.filematches or matchers.files
local dir_matches = clink.dirmatches or matchers.dirs
local files_parser = parser({file_matches})
local dirs_parser = parser({dir_matches})
local looping_files_parser = clink.argmatcher and clink.argmatcher():addarg(clink.filematches):loop()
local function extract_sgr(c)
return c and c:match("^\x1b%[(.*)m$") or c
end
local color_git = "38;2;240;80;50" -- the git orange
local function addicon(m, icon, c)
if matchicons and matchicons.addicontomatch then
if not c and m.type and m.type:find("file") then
if rl.getmatchcolor then
c = extract_sgr(rl.getmatchcolor(m.match, m.type))
end
end
return matchicons.addicontomatch(m, icon, c)
else
return m
end
end
local function addicons(matches)
if matchicons and matchicons.addicontomatch then
for _, m in ipairs(matches) do
local old_type = m.type
m.type = "file"
addicon(m)
m.type = old_type
end
end
return matches
end
local map_file
if rl and rl.getmatchcolor then
map_file = function (file)
if type(file) == "table" then
return file
else
return { match=file, display='\x1b[m'..rl.getmatchcolor(file, 'file')..file, type='arg' }
end
end
else
map_file = function (file)
if type(file) == "table" then
return file
else
return { match=file, display='\x1b[m'..file, type='arg' }
end
end
end
local function has_dot_dirs(token)
for _, t in ipairs(string.explode(token, '/\\')) do
if t == '.' or t == '..' then
return true
end
end
end
local function get_relative_prefix(git_dir)
local cwd = clink.lower(path.join(os.getcwd(), ''))
git_dir = clink.lower(path.join(path.toparent(git_dir), ''))
return cwd:sub(#git_dir + 1)
end
local function adjust_relative_prefix(dir, rel)
local len = string.matchlen(dir, rel)
if len < 0 then
return ''
end
return dir:sub(len + 1)
end
---
-- Lists remote branches based on packed-refs file from git directory
-- @param string [dir] Directory where to search file for
-- @return table List of remote branches
local function list_packed_refs(dir, kind)
local result = w()
local git_dir = dir or git.get_git_common_dir()
if not git_dir then return result end
kind = kind or "remotes"
local packed_refs_file = io.open(git_dir..'/packed-refs')
if packed_refs_file == nil then return {} end
for line in packed_refs_file:lines() do
-- SHA is 40 char length + 1 char for space
if #line > 41 then
local match = line:sub(41):match('refs/'..kind..'/(.*)')
if match then table.insert(result, match) end
end
end
packed_refs_file:close()
return result
end
local function list_remote_branches(dir)
local git_dir = dir or git.get_git_common_dir()
if not git_dir then return w() end
return w(path_module.list_files(git_dir..'/refs/remotes', '/*',
--[[recursive=]]true, --[[reverse_separator=]]true))
:concat(list_packed_refs(git_dir))
:sort():dedupe()
end
local function list_tags(dir)
local git_dir = dir or git.get_git_common_dir()
if not git_dir then return w() end
local result = w(path_module.list_files(git_dir..'/refs/tags', '/*',
--[[recursive=]]true, --[[reverse_separator=]]true))
:concat(list_packed_refs(git_dir, 'tags'))
if string.comparematches then -- luacheck: no global
table.sort(result, string.comparematches) -- luacheck: no global
else
result = result:sort()
end
return result
end
local function list_git_status_files(token, flags) -- luacheck: no unused args
local result = w()
local git_dir = git.get_git_common_dir()
if git_dir then
local rel_pfx = get_relative_prefix(git_dir)
local f = io.popen(git.make_command("status --porcelain "..(flags or "").." **"))
if f then
if string.matchlen then -- luacheck: no global
--[[
token = path.normalise(token)
--]]
for line in f:lines() do
line = line:match("^.[^ ] (.+)$")
if line then
line = path.normalise(line)
--[[
-- TODO: Maybe use match display filtering to show the number of files in each dir?
local mlen = string.matchlen(line, token) -- luacheck: no global
if mlen < 0 then
table.insert(result, { match = line, type = "file" })
else
local dir = path.getdirectory(line:sub(1, mlen))
local child = line:sub(mlen + 1):match("^([^/\\]*[/\\]?)")
local m = dir and path.join(dir, child) or child
local isdir = m:sub(-1):find("[/\\]")
table.insert(result, { match = m, type = (isdir and "dir" or "file") })
end
--]]
table.insert(result, adjust_relative_prefix(line, rel_pfx))
end
end
else
for line in f:lines() do
table.insert(result, adjust_relative_prefix(line:sub(4), rel_pfx))
end
end
f:close()
end
end
return result
end
---
-- Lists local branches for git repo in git_dir directory.
--
-- @param string [dir] Git directory, where to search for remote branches
-- @return table List of branches.
local function list_local_branches(dir)
local git_dir = dir or git.get_git_common_dir()
if not git_dir then return w() end
local result = w(path_module.list_files(git_dir..'/refs/heads', '/*',
--[[recursive=]]true, --[[reverse_separator=]]true))
:concat(list_packed_refs(git_dir, 'heads'))
:sort():dedupe()
return result
end
local function branches()
local git_dir = git.get_git_common_dir()
if not git_dir then return w() end
return list_local_branches(git_dir)
end
-- Function to get the list of git aliases.
local function get_git_aliases()
local res = w()
local f = io.popen(git.make_command("config --get-regexp alias"))
if f == nil then return res end
for line in f:lines() do
local name, command = line:match("^alias.([^ ]+) +(.+)$")
if name then
table.insert(res, { name=name, command=command })
end
end
f:close()
return res
end
-- Function to generate completions for alias
local cached_aliases
local index_aliases = {}
local function alias(token) -- luacheck: no unused args
if cached_aliases then
return cached_aliases
end
local res = w()
local aliases = get_git_aliases()
if clink_version.supports_display_filter_description then
for _, a in ipairs(aliases) do
table.insert(res, { match=a.name, description="Alias: "..a.command })
end
else
for _, a in ipairs(aliases) do
table.insert(res, a.name)
end
end
index_aliases = {}
for _, a in ipairs(aliases) do
index_aliases[a.name] = true
end
if clink.onbeginedit then
cached_aliases = res
end
return res
end
-- Function to generate completions for all command names
local cached_commands
local function catchall(token) -- luacheck: no unused args
if cached_commands then
return cached_commands
end
local res = w()
local f = io.popen(git.make_command("help -a --no-aliases"))
if f then
for line in f:lines() do
local name, desc = line:match("^ ([^ ]+) *(.*)$") -- luacheck: no unused
if name then
-- Currently the descriptions are discarded; only the main
-- commands will list descriptions, so that more columns can
-- fit on the screen.
table.insert(res, name)
end
end
f:close()
end
res:sort()
if clink.onbeginedit then
cached_commands = res
end
return res
end
local function remotes(token) -- luacheck: no unused args
local result = w()
local git_dir = git.get_git_common_dir()
if not git_dir then return result end
local git_config = io.open(git_dir..'/config')
-- if there is no gitconfig file (WAT?!), return empty list
if git_config == nil then return result end
for line in git_config:lines() do
local remote = line:match('%[remote "(.*)"%]')
if (remote) then
table.insert(result, remote)
end
end
git_config:close()
return result
end
local function local_or_remote_branches(token)
-- Try to resolve .git directory location
local git_dir = git.get_git_common_dir()
if not git_dir then return w() end
return list_local_branches(git_dir)
:concat(list_remote_branches(git_dir))
:filter(function(branch)
return clink.is_match(token, branch)
end)
end
local function add_spec_generator(token)
if has_dot_dirs(token) then
return addicons(file_matches(token))
end
return addicons(list_git_status_files(token, "-uall"):map(map_file))
end
local function checkout_spec_generator_049(token)
local function is_token_match(value)
return clink.is_match(token, value)
end
local git_dir = git.get_git_common_dir()
local files = list_git_status_files(token, "-uno"):filter(is_token_match)
local local_branches = branches():filter(is_token_match)
local remote_branches = list_remote_branches(git_dir):filter(is_token_match)
local predicted_branches = list_remote_branches(git_dir)
:map(function (remote_branch)
return remote_branch:match('.-/(.+)')
end)
:filter(function(branch)
return branch
and clink.is_match(token, branch)
-- Filter out those predictions which are already exists as local branches
and not local_branches:contains(branch)
end)
if (#local_branches + #remote_branches + #predicted_branches) == 0 then return files end
-- if there is any refspec that matches token then:
-- * disable readline's filename completion, otherwise we'll get a list of these specs
-- treated as list of files (without 'path' part), ie. 'some_branch' instead of 'my_remote/some_branch'
-- * create display filter for completion table to append path separator to each directory entry
-- since it is not added automatically by readline (see previous point)
clink.matches_are_files(0)
clink.match_display_filter = function ()
local star = '*'
if clink_version.supports_query_rl_var and rl.isvariabletrue('colored-stats') then
star = color.get_clink_color('color.git.star')..star..color.get_clink_color('color.filtered')
end
return files:map(function(file)
return clink.is_dir(file) and file..'\\' or file
end)
:concat(local_branches)
:concat(predicted_branches:map(function(branch) return star..branch end))
:concat(remote_branches)
end
return files
:concat(local_branches)
:concat(predicted_branches)
:concat(remote_branches)
end
local function checkout_spec_generator_usedisplay(token)
-- NOTE: The only reason this needs to use clink.is_match() is because the
-- match_display_filter function defined here ignores the list of matches it
-- receives, which is already filtered correctly and has had duplicates
-- removed.
local function is_token_match(value)
return clink.is_match(token, value)
end
local git_dir = git.get_git_common_dir()
local files = list_git_status_files(token, "-uno"):filter(is_token_match)
local local_branches = branches(token):filter(is_token_match)
local remote_branches = list_remote_branches(git_dir):filter(is_token_match)
local predicted_branches = list_remote_branches(git_dir)
:map(function (remote_branch)
return remote_branch:match('.-/(.+)')
end)
:filter(function(branch)
return branch
and clink.is_match(token, branch)
-- Filter out those predictions which are already exists as local branches
and not local_branches:contains(branch)
end)
-- if there is any refspec that matches token then:
-- * disable readline's filename completion, otherwise we'll get a list of these specs
-- treated as list of files (without 'path' part), ie. 'some_branch' instead of 'my_remote/some_branch'
-- * create display filter for completion table to append path separator to each directory entry
-- since it is not added automatically by readline (see previous point)
clink.match_display_filter = function ()
local star = '*'
if clink_version.supports_query_rl_var and rl.isvariabletrue('colored-stats') then
star = color.get_clink_color('color.git.star')..star..color.get_clink_color('color.filtered')
end
local matches
if clink_version.supports_display_filter_description then
matches = files:map(function(file)
return addicon({ match=file, display='\x1b[m'..file }, "", color_git)
end)
else
matches = files:map(function(file) return '\x1b[m'..file end)
end
return matches
:concat(local_branches:map(function(branch)
return addicon({ match=branch }, "", color_git)
end))
:concat(predicted_branches:map(function(branch)
return addicon({ match=branch, display=star..branch }, "", color_git)
end))
:concat(remote_branches:map(function(branch)
return addicon({ match=branch }, "", color_git)
end))
end
return files
:concat(local_branches)
:concat(predicted_branches)
:concat(remote_branches)
end
local function make_indexed_table(input)
local output = {}
for _, value in ipairs(input) do
output[value] = true
end
return output
end
local function checkout_spec_generator_nosort(token)
local git_dir = git.get_git_common_dir()
local local_branches = branches(token)
local local_branches_idx = make_indexed_table(local_branches)
local remote_branches = list_remote_branches(git_dir)
local remote_branches_idx = make_indexed_table(remote_branches)
local predicted_branches = list_remote_branches(git_dir)
:map(function (remote_branch)
return remote_branch:match('.-/(.+)')
end)
:filter(function(name)
-- Filter out predictions that already exist as local branches.
return not local_branches_idx[name]
end)
local predicted_branches_idx = make_indexed_table(predicted_branches)
local tag_names = list_tags(git_dir)
local files = list_git_status_files(token, "-uno")
:filter(function(name)
name = path.normalise(name, '/')
return not predicted_branches_idx[name] and not remote_branches_idx[name] and not local_branches_idx[name]
end)
local filtered_color = color.get_clink_color('color.filtered')
local local_pre = filtered_color
local predicted_pre = '*'
local remote_pre = filtered_color
local tag_pre = color.get_clink_color('color.doskey')
if clink_version.supports_query_rl_var and rl.isvariabletrue('colored-stats') then
predicted_pre = color.get_clink_color('color.git.star')..predicted_pre..filtered_color
end
local mapped = {
files:map(map_file):map(function (match) return addicon(match, "", color_git) end),
local_branches:map(function(branch)
return addicon({ match=branch, display=local_pre..branch, type='arg' }, "", color_git)
end),
predicted_branches:map(function(branch)
return addicon({ match=branch, display=predicted_pre..branch, type='arg' }, "", color_git)
end),
remote_branches:map(function(branch)
return addicon({ match=branch, display=remote_pre..branch, type='arg' }, "", color_git)
end),
tag_names:map(function(tag)
return addicon({ match=tag, display=tag_pre..tag, type='arg' }, "", extract_sgr(tag_pre))
end),
}
local result = {}
for _, t in ipairs(mapped) do
for _, m in ipairs(t) do
table.insert(result, m)
end
end
result.nosort = true
return result
end
local function checkout_spec_generator(token)
if has_dot_dirs(token) then
return file_matches(token)
end
if clink_version.supports_argmatcher_nosort then
return checkout_spec_generator_nosort(token)
elseif clink_version.supports_display_filter_description then
return checkout_spec_generator_usedisplay(token)
else
return checkout_spec_generator_049(token)
end
end
local function checkout_dashdash(token, _, _, _, user_data)
if user_data and user_data.shared_user_data and user_data.shared_user_data.has_arg1 then
return file_matches(token)
end
if has_dot_dirs(token) then
return file_matches(token)
end
local status_files = list_git_status_files(token, "-uno")
if clink_version.supports_display_filter_description then
return status_files:map(function(file) return { match=file, display='\x1b[m'..file, type='arg' } end)
else
clink.matches_are_files(false)
return status_files
end
end
local function push_branch_spec(token)
local git_dir = git.get_git_common_dir()
if not git_dir then return w() end
local plus_prefix = token:sub(0, 1) == '+'
-- cut out leading '+' symbol as it is a part of branch spec
local branch_spec = plus_prefix and token:sub(2) or token
-- check if there a local/remote branch separator
local s, e = branch_spec:find(':')
-- starting from here we have 2 options:
-- * if there is no branch separator complete word with local branches
if not s then
-- setup display filter to prevent display '+' symbol in completion list
if clink_version.supports_display_filter_description then
local b = branches(branch_spec):map(function(branch)
-- append '+' to results if it was specified
return { match=plus_prefix and '+'..branch or branch, display=branch }
end)
clink.ondisplaymatches(function ()
return b
end)
return b
else
local b = branches(branch_spec)
clink.match_display_filter = function ()
return b
end
return b:map(function(branch)
-- append '+' to results if it was specified
return plus_prefix and '+'..branch or branch
end)
end
else
-- * if there is ':' separator then we need to complete remote branch
local local_branch_spec = branch_spec:sub(1, s - 1)
local remote_branch_spec = branch_spec:sub(e + 1)
-- TODO: show remote branches only for remote that has been specified as previous argument
local b = w(clink.find_dirs(git_dir..'/refs/remotes/*'))
:filter(function(remote) return path_module.is_real_dir(remote) end)
:reduce({}, function(result, remote)
return w(path_module.list_files(git_dir..'/refs/remotes/'..remote, '/*',
--[[recursive=]]true, --[[reverse_separator=]]true))
:filter(function(remote_branch)
return clink.is_match(remote_branch_spec, remote_branch)
end)
:concat(result)
end)
-- setup display filter to prevent display '+' symbol in completion list
if clink_version.supports_display_filter_description then
b = b:map(function(branch)
return {
match=(plus_prefix and '+'..local_branch_spec or local_branch_spec)..':'..branch,
display=branch
}
end)
clink.ondisplaymatches(function ()
return b
end)
return b
else
clink.match_display_filter = function ()
return b
end
return b:map(function(branch)
return (plus_prefix and '+'..local_branch_spec or local_branch_spec)..':'..branch
end)
end
end
end
local stashes = function(token, _, _, builder) -- luacheck: no unused args
local git_dir = git.get_git_dir()
if not git_dir then return w() end
local stash_file = io.open(git_dir..'/logs/refs/stash')
-- if there is no stash file, return empty list
if stash_file == nil then return w() end
local stashes = {}
-- make a dictionary of stash time and stash comment to
-- be able to sort stashes by date/time created
for stash in stash_file:lines() do
local stash_time, stash_name = stash:match('(%d%d%d%d%d%d%d%d%d%d) [+-]%d%d%d%d%s+(.*)')
if (stash_name and stash_name) then
stashes[stash_time] = stash_name
end
end
stash_file:close()
-- get times for available stashes into separate table and sort it
-- from newest to oldest. This is required because of stash@{0}
-- represents _latest_ stash, not the last one in file
local stash_times = {}
for k in pairs(stashes) do
table.insert(stash_times, k)
end
table.sort(stash_times, function (a, b)
return a > b
end)
-- generate matches and match filter table
local ret = {}
local ret_filter = {}
for i,v in ipairs(stash_times) do
local match = "stash@{"..(i-1).."}"
table.insert(ret, match)
if clink_version.supports_display_filter_description then
-- Clink now has a richer match interface. By returning a table,
-- the script is able to provide the stash name separately from the
-- description. If the script does so, then the popup completion
-- window is able to show the stash name plus a dimmed description,
-- but only insert the stash name.
table.insert(ret_filter, { match=match, type="none", description=stashes[v] })
else
table.insert(ret_filter, match.." "..stashes[v])
end
end
local function filter()
return ret_filter
end
if builder and builder.setforcequoting then
builder:setforcequoting()
end
if clink_version.supports_display_filter_description then
clink.ondisplaymatches(filter)
else
clink.match_display_filter = filter
end
return ret
end
local function tags()
local tag_names = list_tags()
local tag_pre = color.get_clink_color('color.doskey')
return tag_names:map(function(tag) return { match=tag, display=tag_pre..tag, type='arg' } end)
end
local cached_guides
local function concept_guides()
if cached_guides then
return cached_guides
end
local matches = {}
local r = io.popen(git.make_command("help -g"))
if r then
local sgr = "\x1b[m"
local mark = " \x1b[22;32m*"
for line in r:lines() do
local guide, desc = line:match("^ ([^ ]+) *(.*)$")
if guide then
if clink_version.supports_display_filter_description then
table.insert(matches, { match=guide, display=sgr..guide..mark, description="Guide: "..desc } )
else
table.insert(matches, guide)
end
end
end
r:close()
end
if clink.onbeginedit then
cached_guides = matches
end
return matches
end
local cached_all_commands
local index_main_commands = {}
local function all_commands()
if cached_all_commands then
return cached_all_commands
end
local matches = {}
local r = io.popen(git.make_command("help -a"))
if r then
local prefix = "Command: "
local mode = {}
for line in r:lines() do
local command, desc = line:match("^ ([^ ]+) *(.*)$")
if command then
if clink_version.supports_display_filter_description then
local mtype = (mode.aliases and "alias") or (index_main_commands[command] and "cmd")
table.insert(matches, { match=command, description=prefix..desc, type=mtype } )
else
table.insert(matches, command)
end
elseif line == "Command aliases" then
prefix = "Alias: "
mode = { aliases=true }
elseif line == "External commands" then
prefix = "External command"
mode = { external=true }
end
end
r:close()
end
if clink.onbeginedit then
cached_all_commands = matches
end
return matches
end
-- luacheck: push
-- luacheck: no max line length
local mergesubtree_arg = parser({dir_matches})
local placeholder_required_arg = parser({})
-- Note: All these separate fromhistory parsers are necessary in order to
-- collect from history separately.
local abbrev_lengths = parser({5, 6, 8, 10, 12, 16, 20, 24, 32, 40})
local batch_format_arg = parser({fromhistory=true, "%(objectname)", "%(objecttype)", "%(objectsize)", "%(objectsize:disk)", "%(deltabase)", "%(rest)"})
local branches_args = parser({branches, hint="branch"}):loop(1)
local clone_filter_arg = parser({fromhistory=true})
local color_opts = parser({"true", "false", "always"})
local commit_trailer_arg = parser({fromhistory=true})
local config_arg = parser({fromhistory=true})
local contextlines_arg = parser({fromhistory=true})
local depth_arg = parser({fromhistory=true})
local diff_filter_arg = parser({fromhistory=true})
local difftool_extcmd_arg = parser({fromhistory=true})
local gpg_keyid_arg = parser({fromhistory=true})
local merge_recursive_options = parser():_addexarg({
--ort and recursive
"ours", "theirs",
"ignore-space-change", "ignore-all-space", "ignore-space-at-eol", "ignore-cr-at-eol",
"renormalize", "no-renormalize",
"find-renames",
{ "find-renames="..placeholder_required_arg, "n", "" },
{ "rename-threshold="..placeholder_required_arg, "n", "" },
"subtree",
{ "subtree="..mergesubtree_arg, "path", "" },
--recursive
"patience",
{ "diff-algorithm="..parser({"patience", "minimal", "histogram", "myers"}), "algorithm", "" },
"no-renames",
})
local merge_strategies = parser({"resolve", "recursive", "ours", "octopus", "subtree"})
local number_commits_arg = parser({"10", "25", "50"})
local origin_arg = parser({fromhistory=true})
local person_arg = parser({fromhistory=true})
local pretty_formats_parser = parser({"oneline", "short", "medium", "full", "fuller", "reference", "email", "mboxrd", "raw", "format:"})
local receive_pack_arg = parser({fromhistory=true})
local regex_ignorelines_arg = parser({fromhistory=true})
local regex_refs_arg = parser({fromhistory=true})
local regex_worddiff_arg = parser({fromhistory=true})
local repo_arg = parser({fromhistory=true})
local shallow_since_arg = parser({fromhistory=true})
local summary_limit_arg = parser({fromhistory=true})
local untracked_files_arg = parser({"no", "normal", "all"})
local x_cmd_arg = parser({fromhistory=true})
local flag__colorequals = "--color="..parser({"always", "auto", "never"})
local flag__columnequals = "--column="..parser({"always", "auto", "never", "column", "row", "plain", "dense", "nodense"})
local flag__conflictequals = '--conflict='..parser({'merge', 'diff3', 'zdiff3'})
local flag__dateequals = "--date="..parser({"relative", "local", "iso", "iso-strict", "rfc", "short", "raw", "human", "unix", "default", "format:", "format-local:"})
local flag__ignore_submodules = "--ignore-submodules="..parser({"none", "untracked", "dirty", "all"})
local flag__whitespaceequals = "--whitespace="..parser({"nowarn", "warn", "fix", "error", "error-all"})
local flagex__abbrevequals = { '--abbrev='..abbrev_lengths, 'n', '' }
local flagex__cleanupequals = { opteq=true, "--cleanup="..parser({"strip", "whitespace", "verbatim", "scissors", "default"}), 'option', '' }
local flagex_c_config = { '-c'..config_arg, ' key=value', 'Set config variable' }
local flagex__config = { '--config'..config_arg, ' key=value', '' }
local flagex__depthdepth = { opteq=true, '--depth'..depth_arg, ' depth', '' }
local flagex__encoding = { opteq=true, '--encoding='..parser({fromhistory=true, "ASCII", "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE"}), 'encoding', '' }
local flagex__gpgsignequals = { '--gpg-sign='..gpg_keyid_arg, 'keyid', '' }
local flagex_s_mergestrategy = { '-s'..merge_strategies, ' strategy', 'Use the given merge strategy' }
local flagex__strategy = { opteq=true, '--strategy'..merge_strategies, ' strategy', '' }
local flagex_u_uploadpack = { '-u'..placeholder_required_arg, ' upload-pack', 'Shortcut for --upload-pack' }
local flagex__uploadpack = { opteq=true, '--upload-pack'..placeholder_required_arg, ' upload-pack', '' }
local flagex_X_strategyoption = { '-X'..merge_recursive_options, ' option', 'Pass option into the merge strategy' }
local flagex__strategyoption = { opteq=true, '--strategy-option'..merge_recursive_options, ' option', '' }
local git_options = {
"core.editor",
"core.pager",
"core.excludesfile",
"core.autocrlf"..parser({"true", "false", "input"}),
"core.trustctime"..parser({"true", "false"}),
"core.whitespace"..parser({
"cr-at-eol",
"-cr-at-eol",
"indent-with-non-tab",
"-indent-with-non-tab",
"space-before-tab",
"-space-before-tab",
"trailing-space",
"-trailing-space"
}),
"commit.template",
"color.ui"..color_opts, "color.*"..color_opts, "color.branch"..color_opts,
"color.diff"..color_opts, "color.interactive"..color_opts, "color.status"..color_opts,
"help.autocorrect",
"merge.tool", "mergetool.*.cmd", "mergetool.trustExitCode"..parser({"true", "false"}), "diff.external",
"user.name", "user.email", "user.signingkey",
}
--------------------------------------------------------------------------------
-- Reusable groups of flags.
local help_flags = {
"--help",
}
local log_flags = {
concat_one_letter_flags=true,
"--decorate", "--decorate="..parser({"short", "full", "auto", "no"}), "--no-decorate",
"--decorate-refs="..regex_refs_arg, "--decorate-refs-exclude="..regex_refs_arg,
"--source",
"--mailmap", "--no-mailmap",
"--full-diff",
"--log-size",
{ "-n"..placeholder_required_arg, " number", "Limit number of commits to output" },
{ opteq=true, "--max-count="..placeholder_required_arg, "number", "" },
{ opteq=true, "--skip="..placeholder_required_arg, "number", "" },
{ opteq=true, "--since="..placeholder_required_arg, "date", "" },
{ opteq=true, "--after="..placeholder_required_arg, "date", "" },
{ opteq=true, "--until="..placeholder_required_arg, "date", "" },
{ opteq=true, "--before="..placeholder_required_arg, "date", "" },
{ opteq=true, "--author="..person_arg, "pattern", "" },
{ opteq=true, "--committer="..person_arg, "pattern", "" },
{ opteq=true, "--grep="..placeholder_required_arg, "pattern", "" },
"--all-match",
"--invert-grep",
{ "-i", "Case insensitive regex matching" },
"--regexp-ignore-case",
"--basic-regexp",
{ "-E", "Use extended regex patterns" },
"--extended-regexp",
{ "-F", "Use fixed strings (no regex patterns)" },
"--fixed-strings",
{ "-P", "Use Perl-compatible regex patterns" },
"--perl-regexp",
"--merges", "--no-merges",
{ opteq=true, "--min-parents="..placeholder_required_arg, "number", "" }, "--no-min-parents",
{ opteq=true, "--max-parents="..placeholder_required_arg, "number", "" }, "--no-max-parents",
"--first-parent",
"--not",
"--all",
{ opteq=true, "--glob="..placeholder_required_arg, "glob", "" },
{ opteq=true, "--exclude="..placeholder_required_arg, "glob", "" },
"--single-worktree",
"--ignore-missing",
"--merge",
}
local log_history_flags = {
concat_one_letter_flags=true,
"--follow",
{ "-L"..parser({fromhistory=true}), " start,end:file", "Trace evolution of range" },
{ "-L:"..parser({fromhistory=true}), "funcname:file", "Trace evolution of function" },
{ opteq=true, "--grep-reflog="..placeholder_required_arg, "pattern", "" },
"--remove-empty",
--"--reflog",
--"--alternate-refs",
"--bisect",
"--stdin",
"--cherry-mark",
"--cherry-pick",
"--left-only",
"--right-only",
"--cherry",
{ "-g", "Walk reflogs, not commit ancestry" },
"--walk-reflogs",
"--boundary",
"--simplify-by-decoration",
"--show-pulls",
"--full-history",
"--dense",
"--sparse",
"--simplify-merges",
"--ancestry-path",
"--date-order",
"--author-date-order",
"--topo-order",
"--reverse",
}
local commit_formatting_flags = {
concat_one_letter_flags=true,
"--pretty",
"--pretty="..pretty_formats_parser,
"--format="..pretty_formats_parser,
"--oneline",
"--abbrev-commit",
"--no-abbrev-commit",
flagex__encoding,
{ "--expand-tabs="..placeholder_required_arg, "n", "" },
"--expand-tabs",
"--no-expand-tabs",
"--notes",
{ "--notes="..placeholder_required_arg, "ref", "" },
"--no-notes",
"--first-parent",
flag__dateequals,
"--parents",
"--children",
"--left-right",
"--graph",
"--show-linear-break",
{ "--show-linear-break="..placeholder_required_arg, "barrier", "" },
}
local diff_flags = {
concat_one_letter_flags=true,
"--no-index",
"--cached",
"--staged",
"--merge-base",
{ "-p", "Generate patch (this is the default)" },
{ "-u", "Generate patch (this is the default)" },
"--patch",
{ "-s", "Suppress diff output" },
"--no-patch",
{ "-U", "n", "Generate diffs with <n> context lines" },
"--unified",
{ opteq=true, "--output="..files_parser },
{ opteq=true, "--output-indicator-new="..placeholder_required_arg, "char", "" },
{ opteq=true, "--output-indicator-old="..placeholder_required_arg, "char", "" },
{ opteq=true, "--output-indicator-context="..placeholder_required_arg, "char", "" },
"--raw",
"--patch-with-raw",
"--indent-heuristic",