-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.lua
More file actions
1265 lines (1198 loc) · 38.9 KB
/
git.lua
File metadata and controls
1265 lines (1198 loc) · 38.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
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
-- Backend implementation for Git.
-- More details at: https://git-scm.com/
local common = require "core.common"
local Backend = require "plugins.scm.backend"
local changes = require "plugins.scm.changes"
---Get top level git directory of path in order to support submodules.
---@param directory string
---@return string
local function git_toplevel_path(directory)
-- direct implementation of: git -C "path" rev-parse --show-toplevel
-- in order to reduce command calls and improve performance
-- REMINDER: previously we used io.popen to call the git command but that
-- causes the command prompt to popup on Windows and is slower
local top_path = directory
repeat
if system.get_file_info(top_path .. PATHSEP .. ".git") then
directory = top_path
break
end
top_path = common.dirname(top_path)
until not top_path
return common.normalize_path(directory:gsub("%s+$", ""))
end
---Similar to git_toplevel_path() but with a base dir and file path
---@param directory string
---@param file? string
---@return string
local function git_repo_dir(directory, file)
if not file then return git_toplevel_path(directory) end
local path = file
local path_info = system.get_file_info(path)
while not path_info or path_info.type == "file" do
path = common.dirname(path)
path_info = system.get_file_info(path)
end
-- if path same as base directory return it
if directory == path then
return directory
end
return git_toplevel_path(path)
end
---@class plugins.scm.backend.git : plugins.scm.backend
---@field super plugins.scm.backend
local Git = Backend:extend()
function Git:new()
self.super.new(self, "Git", "git")
self.watch_dirs = {
-- switch branches
".git",
-- commit changes
".git" .. PATHSEP .. "objects",
".git" .. PATHSEP .. "COMMIT_EDITMSG",
-- pushes
".git" .. PATHSEP .. "refs" .. PATHSEP .. "remotes",
".git" .. PATHSEP .. "refs" .. PATHSEP .. "tags"
}
end
function Git:detect(directory)
if not self.command then return false end
local list = system.list_dir(directory)
if list then
for _, file in ipairs(list) do
if file == ".git" then
return true
end
end
end
return false
end
---@param directory string
function Git:watch_project(directory)
for _, dir in ipairs(self.watch_dirs) do
self.watch:watch(directory .. PATHSEP .. dir)
end
end
---@param directory string
function Git:unwatch_project(directory)
for _, dir in ipairs(self.watch_dirs) do
self.watch:unwatch(directory .. PATHSEP .. dir)
end
end
---@return boolean
function Git:has_staging()
return true
end
---@param directory string Path of project directory
---@return process.options
function Git:get_process_options(directory)
return {
cwd = directory,
env = {
-- Let Git Credential Manager and platform askpass helpers decide how to
-- prompt from the editor instead of forcing terminal-only prompting.
GCM_INTERACTIVE = "auto",
GIT_TERMINAL_PROMPT = "0",
SSH_ASKPASS_REQUIRE = "prefer"
}
}
end
---@param file string Absolute path to file
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
function Git:stage_file(file, directory, callback)
directory = git_repo_dir(directory, file)
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, "add", common.relative_path(directory, file))
end
---@param file string Absolute path to file
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
function Git:unstage_file(file, directory, callback)
directory = git_repo_dir(directory, file)
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, "restore", "--staged", common.relative_path(directory, file))
end
---@param directory string Project directory
---@param callback plugins.scm.backend.ongetstaged
function Git:get_staged(directory, callback)
directory = directory:gsub("[/\\]$", "")
directory = git_repo_dir(directory)
self:execute(function(proc)
---@type table<string,boolean>
local staged = {}
for idx, line in self:get_process_lines(proc, "stdout") do
if line ~= "" then
local trimmed_file = line:gsub("^%s+", ""):gsub("%s+$", "")
staged[common.normalize_path(trimmed_file)] = true
end
if idx % 50 == 0 then
self:yield()
end
end
callback(staged)
end, directory, "--no-optional-locks", "diff", "--name-only", "--cached")
end
---@param directory string Project directory
---@param callback fun(has_changes:boolean)
function Git:has_commit_changes(directory, callback)
self:get_staged(directory, function(staged_files)
for _ in pairs(staged_files or {}) do
callback(true)
return
end
callback(false)
end)
end
---@param callback plugins.scm.backend.ongetbranch
function Git:get_branch(directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local branch = nil
for idx, line in self:get_process_lines(proc, "stdout") do
local result = line:match("^[^%s]+")
if result then
branch = result
break
end
if idx % 50 == 0 then
self:yield()
end
end
callback(branch)
end, directory, "--no-optional-locks", "rev-parse", "--abbrev-ref", "HEAD")
end
---@param directory string
---@param callback plugins.scm.backend.ongetbranches
function Git:get_branches(directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
---@type plugins.scm.backend.branch[]
local branches = {}
for idx, line in self:get_process_lines(proc, "stdout") do
if line ~= "" then
local ref, name, remote, date, commit, message = line:match(
"^(.-)\t(.-)\t(.-)\t(.-)\t(%S+)\t(.*)$"
)
if name and commit and not ref:match("/HEAD$") then
local remote_only = ref:match("^refs/remotes/") ~= nil
table.insert(branches, {
name = name,
remote = remote,
remote_only = remote_only,
date = date,
commit = commit,
message = message
})
end
end
if idx % 50 == 0 then
self:yield()
end
end
callback(branches)
end, directory,
"--no-optional-locks", "for-each-ref", "refs/heads", "refs/remotes",
"--sort=-committerdate",
"--format=%(refname)%09%(refname:short)%09%(upstream:short)%09%(committerdate:short)%09%(objectname)%09%(contents:subject)"
)
end
---@param directory string
---@param callback plugins.scm.backend.ongettags
function Git:get_tags(directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
---@type plugins.scm.backend.tag[]
local tags = {}
for idx, line in self:get_process_lines(proc, "stdout") do
if line ~= "" then
local name, date, peeled_commit, commit, message = line:match(
"^(.-)\t(.-)\t(.-)\t(%S+)\t(.*)$"
)
if name and commit then
local annotated = peeled_commit ~= ""
table.insert(tags, {
name = name,
date = date,
commit = annotated and peeled_commit or commit,
message = message,
annotated = annotated
})
end
end
if idx % 50 == 0 then
self:yield()
end
end
callback(tags)
end, directory,
"--no-optional-locks", "for-each-ref", "refs/tags",
"--sort=-creatordate",
"--format=%(refname:short)%09%(creatordate:short)%09%(*objectname)%09%(objectname)%09%(subject)"
)
end
---@param branch string Branch to create
---@param base_branch string Branch or revision to base the new branch from
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
function Git:create_branch(branch, base_branch, directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, "branch", branch, base_branch)
end
---@param branch string Branch to rebase
---@param base_branch string Branch or revision to rebase onto
---@param directory string Project directory
---@param callback plugins.scm.backend.onrebasestatus
---@param strategy? plugins.scm.backend.rebasestrategy
function Git:rebase_branch(branch, base_branch, directory, callback, strategy)
directory = git_repo_dir(directory)
local params = { "--no-optional-locks", "rebase" }
if strategy == "ours" or strategy == "theirs" then
table.insert(params, "-X")
table.insert(params, strategy)
elseif strategy and strategy ~= "normal" then
callback(false, "Unknown rebase strategy: " .. tostring(strategy), false)
return
end
table.insert(params, base_branch)
table.insert(params, branch)
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg, self:requires_rebase_resolution(errmsg))
end, directory, table.unpack(params))
end
---@return boolean
function Git:supports_rebase_branch()
return true
end
---@param tag string Tag to create
---@param target string Branch, tag, commit or revision to tag
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
---@param annotated? boolean
---@param message? string
function Git:create_tag(tag, target, directory, callback, annotated, message)
directory = git_repo_dir(directory)
local params = { "tag" }
if annotated then
table.insert(params, "-a")
end
table.insert(params, tag)
table.insert(params, target)
if annotated then
table.insert(params, "-m")
table.insert(params, message or "")
end
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, table.unpack(params))
end
---@param tag string Tag to update
---@param old_commit string Commit currently associated with the tag
---@param target string Branch, tag, commit or revision to tag
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
---@param annotated? boolean
---@param message? string
function Git:update_tag(tag, old_commit, target, directory, callback, annotated, message)
directory = git_repo_dir(directory)
local params = { "tag", "-f" }
if annotated then
table.insert(params, "-a")
end
table.insert(params, tag)
table.insert(params, target)
if annotated then
table.insert(params, "-m")
table.insert(params, message or "")
end
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, table.unpack(params))
end
---@param target string Branch, commit or revision to checkout
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
function Git:checkout(target, directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, "checkout", target)
end
---@param commit string Commit hash or revision to cherry-pick
---@param directory string Project directory
---@param callback plugins.scm.backend.oncherrypickstatus
function Git:cherry_pick(commit, directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
if not proc then
callback(false, "Could not start Git process.", false)
return
end
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg, self:requires_cherry_pick_resolution(errmsg))
end, directory, "--no-optional-locks", "cherry-pick", commit)
end
---@return boolean
function Git:supports_cherry_pick()
return true
end
---@param branch string Branch to delete
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
---@param force? boolean Force deletion
function Git:delete_branch(branch, directory, callback, force)
directory = git_repo_dir(directory)
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, "--no-optional-locks", "branch", force and "-D" or "-d", "--", branch)
end
---@param tag string Tag to delete
---@param commit string Commit associated with the tag
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
function Git:delete_tag(tag, commit, directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, "--no-optional-locks", "tag", "-d", "--", tag)
end
---Get list of changes for a git directory or submodule.
---@param self plugins.scm.backend
---@param directory string
---@param changes table Where to store the changes found
---@param callback? function
local function get_changes(self, directory, changes, callback)
self:get_staged(directory, function(staged_files)
self:execute(function(proc)
if not proc then
if callback then callback(false) end
return
end
---@type plugins.scm.backend.filechange[]
local added = {}
for idx, line in self:get_process_lines(proc, "stdout") do
if line ~= "" then
local status, path = line:match("%s*(%S+)%s+(%S+)")
local new_path = nil
if status and path and not added[path] then
if status == "A" then
status = "added"
elseif status == "D" then
status = "deleted"
elseif status == "M" then
status = "edited"
elseif status == "R" then
status = "renamed"
new_path = line:match("%s*%S+%s+%S+%s*%S+%s*(%S+)")
elseif status == "??" then
status = "untracked"
end
table.insert(changes, {
status = status,
staged = staged_files[path] or nil,
path = common.normalize_path(directory .. PATHSEP .. path),
new_path = new_path and common.normalize_path(directory .. PATHSEP .. new_path) or nil
})
added[path] = true
end
end
if idx % 50 == 0 then
self:yield()
end
end
if proc:returncode() ~= 0 then
if callback then callback(false) end
return
end
if callback then
callback(true)
end
end, directory, "--no-optional-locks", "status", "--short")
end)
end
---@param directory string
---@param callback plugins.scm.backend.ongetchanges
function Git:get_changes(directory, callback)
directory = directory:gsub("[/\\]$", "")
directory = git_repo_dir(directory)
local changes = {}
get_changes(self, directory, changes, function(success)
if not success then
callback(nil)
return
end
-- get available submodule changes
self:execute(function(proc)
if not proc then
callback(changes)
return
end
local submodules = {}
local mod_changes_done = {}
for _, line in self:get_process_lines(proc, "stdout") do
local submodule = line:match("%S+%s+(.+)")
if submodule then
submodule = submodule:match("^'(.+)'$")
local submodule_path = common.normalize_path(directory .. PATHSEP .. submodule)
local submodule_info = system.get_file_info(submodule_path)
if
submodule ~= ""
and
submodule_info and submodule_info.type == "dir"
then
table.insert(submodules, submodule)
end
end
end
for idx, submodule in ipairs(submodules) do
local submodule_path = common.normalize_path(directory .. PATHSEP .. submodule)
get_changes(self, submodule_path, changes, function()
mod_changes_done[idx] = true
end)
end
local alldone = false
while not alldone do
alldone = true
for idx, _ in ipairs(submodules) do
if not mod_changes_done[idx] then
alldone = false
break
end
end
self:yield()
end
callback(changes)
end, directory, "submodule", "foreach", "--recursive")
end)
end
---@param path? string
---@param directory string
---@param callback plugins.scm.backend.ongetcommithistory
---@param target? string
---@param target_type? "branch"|"tag"
function Git:get_commit_history(path, directory, callback, target, target_type)
directory = git_repo_dir(directory, path)
local params = {
"log", "--oneline", "--no-decorate",
"--pretty=format:'%an' %H %ct %s"
}
if target then
table.insert(params, target)
end
if path then
table.insert(params, "--")
table.insert(params, common.relative_path(directory, path))
end
self:execute(function(proc)
---@type plugins.scm.backend.commit[]
local history = {}
for idx, line in self:get_process_lines(proc, "stdout") do
local author, hash, date, summary = line:match("('.-') (%S+) (%S+) (.*)")
if author then
table.insert(history, {
author = author:match("'(.*)'"),
hash = hash,
date = os.date("%Y-%m-%d %I:%M %p", tonumber(date)),
summary = summary
})
end
if idx % 100 == 0 then
self:yield()
end
end
callback(history)
end, directory, table.unpack(params))
end
---@param path? string
---@param directory string
---@param callback plugins.scm.backend.ongetcommithistory
---@param target string Branch, tag, commit or revision to include
---@param base? string Branch, tag, commit or revision to exclude
---@param target_type? "branch"|"tag"
function Git:get_commit_range_history(path, directory, callback, target, base, target_type)
directory = git_repo_dir(directory, path)
local params = {
"log", "--oneline", "--no-decorate",
"--pretty=format:'%an' %H %ct %s",
target,
"--not",
base or "HEAD"
}
if path then
table.insert(params, "--")
table.insert(params, common.relative_path(directory, path))
end
self:execute(function(proc)
---@type plugins.scm.backend.commit[]
local history = {}
for idx, line in self:get_process_lines(proc, "stdout") do
local author, hash, date, summary = line:match("('.-') (%S+) (%S+) (.*)")
if author then
table.insert(history, {
author = author:match("'(.*)'"),
hash = hash,
date = os.date("%Y-%m-%d %I:%M %p", tonumber(date)),
summary = summary
})
end
if idx % 100 == 0 then
self:yield()
end
end
callback(history)
end, directory, table.unpack(params))
end
---@param id string
---@param directory string
---@param callback plugins.scm.backend.ongetcommit
function Git:get_commit_info(id, directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
---@type plugins.scm.backend.commit
local commit = {}
for idx, line in self:get_process_lines(proc, "stdout") do
if not commit.hash then
commit.hash = line:match("commit%s+([a-zA-Z0-9]+)$")
elseif not commit.author then
commit.author = line:match("Author:%s+(.+)$")
elseif not commit.date then
commit.date = line:match("Date:%s+(.+)$")
elseif not commit.summary then
commit.summary = line:match(" (.+)")
else
if commit.message then
commit.message = commit.message .. "\n" .. (line:match(" (.+)") or "")
elseif line ~= "" then
local message = line:match(" (.+)")
if message then
commit.message = (line:match(" (.*)") or "")
end
end
if idx % 10 == 0 then self:yield() end
end
end
if commit.message then
commit.message = commit.message:match("(.*)%s+$")
end
callback(commit)
end, directory, "--no-optional-locks", "show", "--no-patch", id)
end
---@param directory string Project directory
---@param callback fun(commit?:string)
function Git:get_current_commit(directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local stdout = self:get_process_output(proc, "stdout")
if not proc or proc:returncode() ~= 0 then
callback(nil)
return
end
callback(stdout:match("^%s*(%S+)"))
end, directory, "--no-optional-locks", "rev-parse", "HEAD")
end
---@param id string
---@param directory string
---@param callback plugins.scm.backend.ongetdiff
function Git:get_commit_diff(id, directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local diff = self:get_process_output(proc, "stdout")
callback(diff)
end, directory, "--no-optional-locks", "show", "-U", id)
end
---@param branch string
---@param head_branch string
---@param directory string
---@param callback plugins.scm.backend.ongetdiff
function Git:get_branch_diff(branch, head_branch, directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local diff = self:get_process_output(proc, "stdout")
callback(diff)
end, directory, "--no-optional-locks", "diff", head_branch .. "..." .. branch)
end
---@param tag string
---@param head_branch string
---@param directory string
---@param callback plugins.scm.backend.ongetdiff
function Git:get_tag_diff(tag, head_branch, directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local diff = self:get_process_output(proc, "stdout")
callback(diff)
end, directory, "--no-optional-locks", "diff", tag .. "..." .. head_branch)
end
---@param id? string
---@param directory string
---@param file string
---@param callback plugins.scm.backend.ongetfile
---@diagnostic disable-next-line
function Git:get_commit_file(id, directory, file, callback)
directory = git_repo_dir(directory)
self:execute(
function(proc)
local content = self:get_process_output(proc, "stdout")
callback(content)
end,
directory,
"--no-optional-locks", "show",
string.format('%s:%s', id or "HEAD", common.relative_path(directory, file))
)
end
---@param directory string
---@param callback plugins.scm.backend.ongetdiff
function Git:get_diff(directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local diff = self:get_process_output(proc, "stdout")
callback(diff)
end, directory, "--no-optional-locks", "diff")
end
---@param file string
---@param callback plugins.scm.backend.ongetdiff
function Git:get_file_diff(file, directory, callback)
directory = git_repo_dir(directory, file)
self:execute(function(proc)
local diff = self:get_process_output(proc, "stdout")
callback(diff)
end, directory, "--no-optional-locks", "diff", common.relative_path(directory, file))
end
---@param file string
---@param directory string
---@param callback plugins.scm.backend.ongetfilestatus
function Git:get_file_status(file, directory, callback)
directory = git_repo_dir(directory, file)
self:execute(function(proc)
local status = "unchanged"
local output = self:get_process_output(proc, "stdout")
for line in output:gmatch("[^\n]+") do
if line ~= "" then
status = line:match("^%s*(%S+)")
if status then
if status == "A" then
status = "added"
elseif status == "D" then
status = "deleted"
elseif status == "M" then
status = "edited"
elseif status == "R" then
status = "renamed"
elseif status == "??" then
status = "untracked"
end
break
end
end
self:yield()
end
callback(status)
end, directory, "--no-optional-locks", "status", "-s", common.relative_path(directory, file))
end
---@param file string
---@param directory string
---@param callback plugins.scm.backend.ongetfileblame
function Git:get_file_blame(file, directory, callback)
directory = git_repo_dir(directory, file)
self:execute(function(proc)
---@type plugins.scm.backend.blame[]
local list = {}
for idx, line in self:get_process_lines(proc, "stdout") do
if line ~= "" then
local commit, author, date = line:match(
"^%^?([A-Fa-f0-9]+) %((.-) (%d%d%d%d%-%d%d%-%d%d)"
)
if commit then
table.insert(list, {
commit = commit,
author = author:match("^%s*(.-)%s*$"), -- trim spaces
date = date
})
end
end
if idx % 100 == 0 then
self:yield()
end
end
callback(#list > 0 and list or nil)
end, directory, "--no-optional-locks", "blame", common.relative_path(directory, file))
end
---@param callback plugins.scm.backend.ongetstats
function Git:get_stats(directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local diff = self:get_process_output(proc, "stdout")
callback(changes.stats(diff, function() self:yield() end))
end, directory, "--no-optional-locks", "diff", "-U0")
end
---@param directory string Project directory
---@param callback plugins.scm.backend.ongetstatus
function Git:get_status(directory, callback)
directory = git_repo_dir(directory)
self:execute(function(proc)
local status = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if stderr ~= "" then
status = stderr
elseif stdout ~= "" then
status = stdout
end
callback(status)
end, directory, "--no-optional-locks", "status")
end
---@param directory string Project directory
---@param message string Commit message
---@param callback plugins.scm.backend.onnewcommit
function Git:new_commit(directory, message, callback)
directory = git_repo_dir(directory)
self:with_commit_message_file(message, function(filename, cleanup, errmsg)
if not filename then
callback(false, errmsg or "Could not create temporary commit message file.")
return
end
local started = self:execute(function(proc)
self:handle_exec_status(proc, callback)
if cleanup then cleanup() end
end, directory, "--no-optional-locks", "commit", "-F", filename)
if not started and cleanup then cleanup() end
end)
end
---@param directory string Project directory
---@param commit string Current commit hash
---@param message string Commit message
---@param callback plugins.scm.backend.onexecstatus
function Git:amend_commit(directory, commit, message, callback)
directory = git_repo_dir(directory)
self:with_commit_message_file(message, function(filename, cleanup, errmsg)
if not filename then
callback(false, errmsg or "Could not create temporary commit message file.")
return
end
local started = self:execute(function(proc)
self:handle_exec_status(proc, callback)
if cleanup then cleanup() end
end, directory, "--no-optional-locks", "commit", "--amend", "-F", filename)
if not started and cleanup then cleanup() end
end)
end
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
---@param username? string Ignored by Git
---@param password? string Ignored by Git
---@param strategy? "merge"|"rebase"|"ff-only"
function Git:pull(directory, callback, username, password, strategy)
directory = git_repo_dir(directory)
local params = { "pull" }
if strategy == "merge" then
table.insert(params, "--no-rebase")
elseif strategy == "rebase" then
table.insert(params, "--rebase")
elseif strategy == "ff-only" then
table.insert(params, "--ff-only")
end
self:execute(function(proc)
local success = false
local errmsg = ""
local stdout = self:get_process_output(proc, "stdout")
local stderr = self:get_process_output(proc, "stderr")
if proc and proc:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg, false, self:requires_pull_strategy(errmsg))
end, directory, table.unpack(params))
end
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
---@param username? string Ignored by Git
---@param password? string Ignored by Git
function Git:push(directory, callback, username, password)
directory = git_repo_dir(directory)
self:execute(function(proc)
self:handle_exec_status(proc, callback)