Skip to content

Commit 6a82dfc

Browse files
committed
chore: Misc housekeeping.
1 parent 73271db commit 6a82dfc

File tree

12 files changed

+115
-114
lines changed

12 files changed

+115
-114
lines changed

lua/diffview/actions.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
local lazy = require("diffview.lazy")
22

33
local DiffView = lazy.access("diffview.scene.views.diff.diff_view", "DiffView") ---@type DiffView|LazyModule
4-
local HelpPanel = lazy.access("diffview.ui.panels.help_panel", "HelpPanel") ---@type HelpPanel|LazyModule
54
local FileHistoryView = lazy.access("diffview.scene.views.file_history.file_history_view", "FileHistoryView") ---@type FileHistoryView|LazyModule
5+
local HelpPanel = lazy.access("diffview.ui.panels.help_panel", "HelpPanel") ---@type HelpPanel|LazyModule
66
local StandardView = lazy.access("diffview.scene.views.standard.standard_view", "StandardView") ---@type StandardView|LazyModule
7-
local vcs = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
87
local lib = lazy.require("diffview.lib") ---@module "diffview.lib"
98
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
9+
local vcs_utils = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
1010

1111
local Diff1 = lazy.access("diffview.scene.layouts.diff_1", "Diff1") ---@type Diff1|LazyModule
1212
local Diff2Hor = lazy.access("diffview.scene.layouts.diff_2_hor", "Diff2Hor") ---@type Diff2Hor|LazyModule
@@ -167,7 +167,7 @@ function M.next_conflict()
167167
local curfile = main.file
168168

169169
if main:is_valid() and curfile:is_valid() then
170-
local conflicts, _, cur_idx = vcs.parse_conflicts(
170+
local conflicts, _, cur_idx = vcs_utils.parse_conflicts(
171171
api.nvim_buf_get_lines(curfile.bufnr, 0, -1, false),
172172
main.id
173173
)
@@ -198,7 +198,7 @@ function M.prev_conflict()
198198
local curfile = main.file
199199

200200
if main:is_valid() and curfile:is_valid() then
201-
local conflicts, _, cur_idx = vcs.parse_conflicts(
201+
local conflicts, _, cur_idx = vcs_utils.parse_conflicts(
202202
api.nvim_buf_get_lines(curfile.bufnr, 0, -1, false),
203203
main.id
204204
)
@@ -335,7 +335,7 @@ function M.conflict_choose(target)
335335
local curfile = main.file
336336

337337
if main:is_valid() and curfile:is_valid() then
338-
local _, cur = vcs.parse_conflicts(
338+
local _, cur = vcs_utils.parse_conflicts(
339339
api.nvim_buf_get_lines(curfile.bufnr, 0, -1, false),
340340
main.id
341341
)

lua/diffview/api/views/diff/diff_view.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local FilePanel = lazy.access("diffview.scene.views.diff.file_panel", "FilePanel
66
local Rev = lazy.access("diffview.vcs.adapters.git.rev", "GitRev") ---@type GitRev|LazyModule
77
local RevType = lazy.access("diffview.vcs.rev", "RevType") ---@type RevType|LazyModule
88
local async = lazy.require("plenary.async") ---@module "plenary.async"
9-
local vcs = lazy.require("diffview.vcs") ---@module "diffview.vcs"
9+
local vcs_utils = lazy.require("diffview.vcs") ---@module "diffview.vcs"
1010
local logger = lazy.require("diffview.logger") ---@module "diffview.logger"
1111
local oop = lazy.require("diffview.oop") ---@module "diffview.oop"
1212
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
@@ -34,7 +34,7 @@ function CDiffView:init(opt)
3434
logger.info("[api] Creating a new Custom DiffView.")
3535
self.valid = false
3636

37-
local err, adapter = vcs.get_adapter({ top_indicators = { opt.git_root } })
37+
local err, adapter = vcs_utils.get_adapter({ top_indicators = { opt.git_root } })
3838

3939
if err then
4040
utils.err(

lua/diffview/init.lua

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ end
55
local hl = require("diffview.hl")
66
local lazy = require("diffview.lazy")
77

8-
---@module "diffview.arg_parser"
9-
local arg_parser = lazy.require("diffview.arg_parser")
10-
---@module "diffview.config"
11-
local config = lazy.require("diffview.config")
12-
---@module "diffview.vcs"
13-
local vcs = lazy.require("diffview.vcs")
14-
---@module "diffview.lib"
15-
local lib = lazy.require("diffview.lib")
16-
---@module "diffview.logger"
17-
local logger = lazy.require("diffview.logger")
18-
---@module "diffview.utils"
19-
local utils = lazy.require("diffview.utils")
8+
local arg_parser = lazy.require("diffview.arg_parser") ---@module "diffview.arg_parser"
9+
local config = lazy.require("diffview.config") ---@module "diffview.config"
10+
local lib = lazy.require("diffview.lib") ---@module "diffview.lib"
11+
local logger = lazy.require("diffview.logger") ---@module "diffview.logger"
12+
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
13+
local vcs = lazy.require("diffview.vcs") ---@module "diffview.vcs"
2014

2115
local api = vim.api
2216

lua/diffview/scene/file_entry.lua

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
local lazy = require("diffview.lazy")
22
local oop = require("diffview.oop")
33

4-
---@type vcs.File|LazyModule
5-
local File = lazy.access("diffview.vcs.file", "File")
6-
---@type RevType|LazyModule
7-
local RevType = lazy.access("diffview.vcs.rev", "RevType")
8-
---@type Diff1|LazyModule
9-
local Diff1 = lazy.access("diffview.scene.layouts.diff_1", "Diff1")
10-
---@type Diff2|LazyModule
11-
local Diff2 = lazy.access("diffview.scene.layouts.diff_2", "Diff2")
12-
---@type Diff3|LazyModule
13-
local Diff3 = lazy.access("diffview.scene.layouts.diff_3", "Diff3")
14-
---@type Diff4|LazyModule
15-
local Diff4 = lazy.access("diffview.scene.layouts.diff_4", "Diff4")
16-
---@module "diffview.utils"
17-
local utils = lazy.require("diffview.utils")
4+
local Diff1 = lazy.access("diffview.scene.layouts.diff_1", "Diff1") ---@type Diff1|LazyModule
5+
local Diff2 = lazy.access("diffview.scene.layouts.diff_2", "Diff2") ---@type Diff2|LazyModule
6+
local Diff3 = lazy.access("diffview.scene.layouts.diff_3", "Diff3") ---@type Diff3|LazyModule
7+
local Diff4 = lazy.access("diffview.scene.layouts.diff_4", "Diff4") ---@type Diff4|LazyModule
8+
local File = lazy.access("diffview.vcs.file", "File") ---@type vcs.File|LazyModule
9+
local RevType = lazy.access("diffview.vcs.rev", "RevType") ---@type RevType|LazyModule
10+
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
1811

1912
local M = {}
2013

@@ -36,7 +29,7 @@ local fstat_cache = {}
3629
---@field layout Layout
3730
---@field status string
3831
---@field stats GitStats
39-
---@field kind git.FileKind
32+
---@field kind vcs.FileKind
4033
---@field commit Commit|nil
4134
---@field active boolean
4235
local FileEntry = oop.create_class("FileEntry")
@@ -48,7 +41,7 @@ local FileEntry = oop.create_class("FileEntry")
4841
---@field layout Layout
4942
---@field status string
5043
---@field stats GitStats
51-
---@field kind git.FileKind
44+
---@field kind vcs.FileKind
5245
---@field commit? Commit
5346

5447
---FileEntry constructor

lua/diffview/scene/views/diff/diff_view.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ local config = lazy.require("diffview.config") ---@module "diffview.config"
1616
local debounce = lazy.require("diffview.debounce") ---@module "diffview.debounce"
1717
local logger = lazy.require("diffview.logger") ---@module "diffview.logger"
1818
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
19-
local vcs = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
19+
local vcs_utils = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
2020

2121
local api = vim.api
2222
local M = {}
@@ -76,7 +76,7 @@ function DiffView:init(opt)
7676
local file = entry.layout:get_main_win().file
7777

7878
local count_conflicts = vim.schedule_wrap(function()
79-
local conflicts = vcs.parse_conflicts(api.nvim_buf_get_lines(file.bufnr, 0, -1, false))
79+
local conflicts = vcs_utils.parse_conflicts(api.nvim_buf_get_lines(file.bufnr, 0, -1, false))
8080

8181
entry.stats = entry.stats or {}
8282
entry.stats.conflicts = #conflicts
@@ -277,7 +277,7 @@ end
277277
---@return string[] err
278278
---@return FileDict
279279
DiffView.get_updated_files = async.wrap(function(self, callback)
280-
vcs.diff_file_list(
280+
vcs_utils.diff_file_list(
281281
self.adapter,
282282
self.left,
283283
self.right,
@@ -304,7 +304,7 @@ DiffView.update_files = debounce.debounce_trailing(100, true, vim.schedule_wrap(
304304
-- If left is tracking HEAD and right is LOCAL: Update HEAD rev.
305305
local new_head
306306
if self.left.track_head and self.right.type == RevType.LOCAL then
307-
new_head = vcs.head_rev(self.adapter.ctx.toplevel)
307+
new_head = self.adapter:head_rev()
308308
if new_head and self.left.commit ~= new_head.commit then
309309
self.left = new_head
310310
else

lua/diffview/scene/views/diff/listeners.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local RevType = lazy.access("diffview.vcs.rev", "RevType") ---@type RevType|Lazy
55
local actions = lazy.require("diffview.actions") ---@module "diffview.actions"
66
local async = lazy.require("plenary.async") ---@module "plenary.async"
77
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
8-
local vcs = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
8+
local vcs_utils = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
99

1010
local api = vim.api
1111

@@ -220,7 +220,7 @@ return function(view)
220220
utils.err("The file is open with unsaved changes! Aborting file restoration.")
221221
return
222222
end
223-
vcs.restore_file(view.adapter, file.path, file.kind, commit, function()
223+
vcs_utils.restore_file(view.adapter, file.path, file.kind, commit, function()
224224
async.util.scheduler()
225225
view:update_files()
226226
end)

lua/diffview/ui/models/file_tree/file_tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local M = {}
88
---@class DirData
99
---@field name string
1010
---@field path string
11-
---@field kind git.FileKind
11+
---@field kind vcs.FileKind
1212
---@field collapsed boolean
1313
---@field status string
1414
---@field _node Node

lua/diffview/vcs/adapter.lua

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
local oop = require('diffview.oop')
2-
local utils = require('diffview.utils')
3-
local logger = require('diffview.logger')
4-
local arg_parser = require('diffview.arg_parser')
5-
local RevType = require("diffview.vcs.rev").RevType
6-
local Rev = require("diffview.vcs.rev").Rev
1+
local Job = require("plenary.job")
2+
local async = require("plenary.async")
3+
local lazy = require("diffview.lazy")
4+
local oop = require("diffview.oop")
5+
6+
local JobStatus = lazy.access("diffview.vcs.utils", "JobStatus") ---@type JobStatus|LazyModule
7+
local Rev = lazy.access("diffview.vcs.rev", "Rev") ---@type Rev|LazyModule
8+
local RevType = lazy.access("diffview.vcs.rev", "RevType") ---@type RevType|LazyModule
9+
local arg_parser = lazy.require("diffview.arg_parser") ---@module "diffview.arg_parser"
10+
local logger = lazy.require("diffview.logger") ---@module "diffview.logger"
11+
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
12+
local vcs_utils = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
713

814
local M = {}
915

1016
---@class vcs.adapter.LayoutOpt
1117
---@field default_layout Diff2
1218
---@field merge_layout Layout
1319

14-
1520
---@class vcs.adapter.VCSAdapter.Bootstrap
1621
---@field done boolean # Did the bootstrapping
1722
---@field ok boolean # Bootstrapping was successful
@@ -33,7 +38,7 @@ local M = {}
3338
---@field bootstrap vcs.adapter.VCSAdapter.Bootstrap
3439
---@field ctx vcs.adapter.VCSAdapter.Ctx
3540
---@field flags vcs.adapter.VCSAdapter.Flags
36-
local VCSAdapter = oop.create_class('VCSAdapter')
41+
local VCSAdapter = oop.create_class("VCSAdapter")
3742

3843
VCSAdapter.Rev = Rev
3944

@@ -271,15 +276,59 @@ end
271276

272277
---Restore file
273278
---@param path string
274-
---@param kind '"staged"' | '"working"'
279+
---@param kind vcs.FileKind
275280
---@param commit string?
276-
---@return boolean # Restore was successful
281+
---@return boolean success
282+
---@return string? undo # If the adapter supports it: a command that will undo the restoration.
277283
function VCSAdapter:file_restore(path, kind, commit)
278284
oop.abstract_stub()
279285
end
280286

281287
---@diagnostic enable: unused-local, missing-return
282288

289+
---@param self VCSAdapter
290+
---@param args string[]
291+
---@param callback fun(stderr: string[]?, stdout: string[]?)
292+
VCSAdapter.show = async.wrap(function(self, args, callback)
293+
local job = Job:new({
294+
command = self:bin(),
295+
args = self:get_show_args(args),
296+
cwd = self.ctx.toplevel,
297+
---@type Job
298+
on_exit = async.void(function(j)
299+
local context = "vcs.utils.show()"
300+
utils.handle_job(j, {
301+
fail_on_empty = true,
302+
context = context,
303+
debug_opt = { no_stdout = true, context = context },
304+
})
305+
306+
if j.code ~= 0 then
307+
callback(j:stderr_result() or {}, nil)
308+
return
309+
end
310+
311+
local out_status
312+
313+
if #j:result() == 0 then
314+
async.util.scheduler()
315+
out_status = vcs_utils.ensure_output(2, { j }, context)
316+
end
317+
318+
if out_status == JobStatus.ERROR then
319+
callback(j:stderr_result() or {}, nil)
320+
return
321+
end
322+
323+
callback(nil, j:result())
324+
end),
325+
})
326+
-- Problem: Running multiple 'show' jobs simultaneously may cause them to fail
327+
-- silently.
328+
-- Solution: queue them and run them one after another.
329+
vcs_utils.queue_sync_job(job)
330+
end, 3)
331+
283332
---Convert revs to string representation.
284333
---@param left Rev
285334
---@param right Rev

lua/diffview/vcs/adapters/git/init.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,9 @@ function GitAdapter:get_files_args(args)
12271227
return utils.vec_join(self:args(), "ls-files", "--others", "--exclude-standard", args)
12281228
end
12291229

1230+
---@param path string
1231+
---@param kind vcs.FileKind
1232+
---@param commit string?
12301233
function GitAdapter:file_restore(path, kind, commit)
12311234
local out, code
12321235
local abs_path = utils.path:join(self.ctx.toplevel, path)
@@ -1245,7 +1248,7 @@ function GitAdapter:file_restore(path, kind, commit)
12451248
out, code = self:exec_sync({ "hash-object", "-w", "--", path }, self.ctx.toplevel)
12461249
if code ~= 0 then
12471250
utils.err("Failed to write file blob into the object database. Aborting file restoration.", true)
1248-
return nil
1251+
return false
12491252
end
12501253
end
12511254

@@ -1272,7 +1275,7 @@ function GitAdapter:file_restore(path, kind, commit)
12721275
end
12731276
end
12741277

1275-
if kind == "working" then
1278+
if kind == "working" or kind == "conflicting" then
12761279
-- File is untracked and has no history: delete it from fs.
12771280
local ok, err = utils.path:unlink(abs_path)
12781281
if not ok then
@@ -1281,7 +1284,7 @@ function GitAdapter:file_restore(path, kind, commit)
12811284
:format(abs_path),
12821285
err
12831286
}, true)
1284-
return nil
1287+
return false
12851288
end
12861289
else
12871290
-- File only exists in index
@@ -1298,7 +1301,7 @@ function GitAdapter:file_restore(path, kind, commit)
12981301
)
12991302
end
13001303

1301-
return undo
1304+
return true, undo
13021305
end
13031306

13041307
function GitAdapter:reset_files(paths)

lua/diffview/vcs/file.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ local RevType = lazy.access("diffview.vcs.rev", "RevType") ---@type RevType|Lazy
66
local async = lazy.require("plenary.async") ---@module "plenary.async"
77
local config = lazy.require("diffview.config") ---@module "diffview.config"
88
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
9-
local vcs = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
109

1110
local pl = lazy.access(utils, "path") ---@type PathLib|LazyModule
1211

1312
local api = vim.api
1413
local M = {}
1514

16-
---@alias git.FileDataProducer fun(kind: git.FileKind, path: string, pos: "left"|"right"): string[]
15+
---@alias git.FileDataProducer fun(kind: vcs.FileKind, path: string, pos: "left"|"right"): string[]
1716

1817
---@class vcs.File : diffview.Object
1918
---@field adapter GitAdapter
@@ -22,7 +21,7 @@ local M = {}
2221
---@field parent_path string
2322
---@field basename string
2423
---@field extension string
25-
---@field kind git.FileKind
24+
---@field kind vcs.FileKind
2625
---@field nulled boolean
2726
---@field rev Rev
2827
---@field commit Commit?
@@ -201,8 +200,7 @@ function File:create_buffer(callback)
201200
end, nil)
202201

203202
else
204-
vcs.show(
205-
self.adapter,
203+
self.adapter:show(
206204
{ ("%s:%s"):format(self.rev:object_name() or "", self.path) },
207205
function(err, result)
208206
if err then

0 commit comments

Comments
 (0)