-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.lua
596 lines (563 loc) · 18 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
-- Backend implementation for Git.
-- More details at: https://git-scm.com/
local common = require "core.common"
local Backend = require "plugins.scm.backend"
---Get top level git directory of path in order to support submodules.
---@param self plugins.scm.backend
---@param path string
---@return string
local function git_toplevel_path(self, path)
local git_command = io.popen(
self.command .. " -C " .. "\"" .. path .. "\" "
.. "rev-parse --show-toplevel",
"r"
)
if git_command then
path = git_command:read("*a")
git_command:close()
end
return common.normalize_path(path:gsub("%s+$", ""))
end
---Similar to git_toplevel_path() but with a base dir and file path
---@param self plugins.scm.backend
---@param directory string
---@param file? string
---@return string
local function git_repo_dir(self, directory, file)
if not file then return git_toplevel_path(self, 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
-- prevent executing git if path same as base directory
if directory == path then
return directory
end
return git_toplevel_path(self, 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",
-- pushes
".git" .. PATHSEP .. "refs" .. PATHSEP .. "remotes"
}
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(dir)
end
end
---@param directory string
function Git:unwatch_project(directory)
for _, dir in ipairs(self.watch_dirs) do
self.watch:unwatch(dir)
end
end
---@return boolean
function Git:has_staging()
return true
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(self, 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: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(self, 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: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(self, 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 callback plugins.scm.backend.ongetbranch
function Git:get_branch(directory, callback)
directory = git_repo_dir(self, 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
---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)
---@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 callback then
callback()
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(self, directory)
local changes = {}
get_changes(self, directory, changes, function()
-- get available submodule changes
self:execute(function(proc)
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
function Git:get_commit_history(path, directory, callback)
directory = git_repo_dir(self, directory, path)
local params = {
"log", "--oneline", "--no-decorate",
"--pretty=format:'%an' %H %ct %s"
}
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(self, 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 id string
---@param directory string
---@param callback plugins.scm.backend.ongetdiff
function Git:get_commit_diff(id, directory, callback)
directory = git_repo_dir(self, 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 directory string
---@param callback plugins.scm.backend.ongetdiff
function Git:get_diff(directory, callback)
directory = git_repo_dir(self, 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(self, 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(self, 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(self, 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(self, directory)
self:execute(function(proc)
local inserts = 0
local deletes = 0
for idx, line in self:get_process_lines(proc, "stdout") do
if line ~= "" then
local i, d = line:match("%s*(%d+)%s+(%d+)")
inserts = inserts + (tonumber(i) or 0)
deletes = deletes + (tonumber(d) or 0)
end
if idx % 50 == 0 then
self:yield()
end
end
local stats = {inserts = inserts, deletes = deletes}
callback(stats)
end, directory, "--no-optional-locks", "diff", "--numstat")
end
---@param directory string Project directory
---@param callback plugins.scm.backend.ongetstatus
function Git:get_status(directory, callback)
directory = git_repo_dir(self, 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 callback plugins.scm.backend.onexecstatus
function Git:pull(directory, callback)
directory = git_repo_dir(self, 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:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, "pull")
end
---@param file string Absolute path to file
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
function Git:revert_file(file, directory, callback)
directory = git_repo_dir(self, 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:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, "restore", common.relative_path(directory, file))
end
---@param path string Absolute path to file
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
function Git:add_path(path, directory, callback)
directory = git_repo_dir(self, directory, path)
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: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, path))
end
---@param path string Absolute path to file
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
function Git:remove_path(path, directory, callback)
directory = git_repo_dir(self, directory, path)
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:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end, directory, "rm", "-r", "--cached", common.relative_path(directory, path))
end
---@param from string Path to move
---@param to string Destination of from path
---@param directory string Project directory
---@param callback plugins.scm.backend.onexecstatus
function Git:move_path(from, to, directory, callback)
directory = git_repo_dir(self, directory, from)
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:returncode() == 0 then
success = true
else
if stderr ~= "" then
errmsg = stderr
elseif stdout ~= "" then
errmsg = stdout
end
end
callback(success, errmsg)
end,
directory, "mv",
common.relative_path(directory, from),
common.relative_path(directory, to)
)
end
return Git