Skip to content

Commit ff9ffee

Browse files
zegervdvsindrets
andauthored
refactor: Refactor git module into vcs-type agnostic module (#235)
Co-authored-by: Sindre T. Strøm <sindrets@gmail.com>
1 parent 94a3422 commit ff9ffee

37 files changed

+3116
-2687
lines changed

lua/diffview/actions.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local lazy = require("diffview.lazy")
33
local DiffView = lazy.access("diffview.scene.views.diff.diff_view", "DiffView") ---@type DiffView|LazyModule
44
local FileHistoryView = lazy.access("diffview.scene.views.file_history.file_history_view", "FileHistoryView") ---@type FileHistoryView|LazyModule
55
local StandardView = lazy.access("diffview.scene.views.standard.standard_view", "StandardView") ---@type StandardView|LazyModule
6-
local git = lazy.require("diffview.git.utils") ---@module "diffview.git.utils"
6+
local vcs = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
77
local lib = lazy.require("diffview.lib") ---@module "diffview.lib"
88
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
99

@@ -166,7 +166,7 @@ function M.next_conflict()
166166
local curfile = main.file
167167

168168
if main:is_valid() and curfile:is_valid() then
169-
local conflicts, _, cur_idx = git.parse_conflicts(
169+
local conflicts, _, cur_idx = vcs.parse_conflicts(
170170
api.nvim_buf_get_lines(curfile.bufnr, 0, -1, false),
171171
main.id
172172
)
@@ -197,7 +197,7 @@ function M.prev_conflict()
197197
local curfile = main.file
198198

199199
if main:is_valid() and curfile:is_valid() then
200-
local conflicts, _, cur_idx = git.parse_conflicts(
200+
local conflicts, _, cur_idx = vcs.parse_conflicts(
201201
api.nvim_buf_get_lines(curfile.bufnr, 0, -1, false),
202202
main.id
203203
)
@@ -334,7 +334,7 @@ function M.conflict_choose(target)
334334
local curfile = main.file
335335

336336
if main:is_valid() and curfile:is_valid() then
337-
local _, cur = git.parse_conflicts(
337+
local _, cur = vcs.parse_conflicts(
338338
api.nvim_buf_get_lines(curfile.bufnr, 0, -1, false),
339339
main.id
340340
)

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ local lazy = require("diffview.lazy")
33
local DiffView = lazy.access("diffview.scene.views.diff.diff_view", "DiffView") ---@type DiffView|LazyModule
44
local FileEntry = lazy.access("diffview.scene.file_entry", "FileEntry") ---@type FileEntry|LazyModule
55
local FilePanel = lazy.access("diffview.scene.views.diff.file_panel", "FilePanel") ---@type FilePanel|LazyModule
6-
local Rev = lazy.access("diffview.git.rev", "Rev") ---@type Rev|LazyModule
7-
local RevType = lazy.access("diffview.git.rev", "RevType") ---@type RevType|LazyModule
6+
local Rev = lazy.access("diffview.vcs.adapters.git.rev", "GitRev") ---@type GitRev|LazyModule
7+
local RevType = lazy.access("diffview.vcs.rev", "RevType") ---@type RevType|LazyModule
88
local async = lazy.require("plenary.async") ---@module "plenary.async"
9-
local git = lazy.require("diffview.git.utils") ---@module "diffview.git.utils"
9+
local vcs = 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"
@@ -33,16 +33,19 @@ local CDiffView = oop.create_class("CDiffView", DiffView.__get())
3333
function CDiffView:init(opt)
3434
logger.info("[api] Creating a new Custom DiffView.")
3535
self.valid = false
36-
local git_dir = git.git_dir(opt.git_root)
3736

38-
if not git_dir then
37+
local err, adapter = vcs.get_adapter({ top_indicators = { opt.git_root } })
38+
39+
if err then
3940
utils.err(
40-
("Failed to find the git dir for the repository: %s")
41+
("Failed to create an adapter for the repository: %s")
4142
:format(utils.str_quote(opt.git_root))
4243
)
4344
return
4445
end
4546

47+
---@cast adapter -?
48+
4649
-- Fix malformed revs
4750
for _, v in ipairs({ "left", "right" }) do
4851
local rev = opt[v]
@@ -54,18 +57,13 @@ function CDiffView:init(opt)
5457
self.fetch_files = opt.update_files
5558
self.get_file_data = opt.get_file_data
5659

57-
local git_ctx = {
58-
toplevel = opt.git_root,
59-
dir = git_dir,
60-
}
61-
6260
CDiffView:super().init(self, vim.tbl_extend("force", opt, {
63-
git_ctx = git_ctx,
61+
adapter = adapter,
6462
panel = FilePanel(
65-
git_ctx,
63+
adapter,
6664
self.files,
6765
self.path_args,
68-
self.rev_arg or git.rev_to_pretty_string(opt.left, opt.right)
66+
self.rev_arg or adapter:rev_to_pretty_string(opt.left, opt.right)
6967
),
7068
}))
7169

@@ -128,7 +126,7 @@ function CDiffView:create_file_entries(files)
128126
{
129127
kind = "staged",
130128
files = files.staged or {},
131-
left = git.head_rev(self.git_ctx.toplevel),
129+
left = self.adapter:head_rev(),
132130
right = Rev(RevType.STAGE, 0),
133131
},
134132
}
@@ -139,7 +137,7 @@ function CDiffView:create_file_entries(files)
139137
for _, file_data in ipairs(v.files) do
140138
if v.kind == "conflicting" then
141139
table.insert(entries[v.kind], FileEntry.with_layout(CDiffView.get_default_merge_layout(), {
142-
git_ctx = self.git_ctx,
140+
adapter = self.adapter,
143141
path = file_data.path,
144142
oldpath = file_data.oldpath,
145143
status = "U",
@@ -148,11 +146,10 @@ function CDiffView:create_file_entries(files)
148146
rev_main = Rev(RevType.LOCAL),
149147
rev_theirs = Rev(RevType.STAGE, 3),
150148
rev_base = Rev(RevType.STAGE, 1),
151-
get_data = self.get_file_data,
152149
}))
153150
else
154151
table.insert(entries[v.kind], FileEntry.for_d2(CDiffView.get_default_diff2(), {
155-
git_ctx = self.git_ctx,
152+
adapter = self.adapter,
156153
path = file_data.path,
157154
oldpath = file_data.oldpath,
158155
status = file_data.status,

0 commit comments

Comments
 (0)