forked from tanvirtin/vgit.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.lua
More file actions
284 lines (225 loc) · 7.75 KB
/
Copy pathModel.lua
File metadata and controls
284 lines (225 loc) · 7.75 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
local fs = require('vgit.core.fs')
local Diff = require('vgit.core.Diff')
local loop = require('vgit.core.loop')
local utils = require('vgit.core.utils')
local Object = require('vgit.core.Object')
local git_show = require('vgit.git.git_show')
local git_repo = require('vgit.git.git_repo')
local git_hunks = require('vgit.git.git_hunks')
local GitFile = require('vgit.git.GitFile')
local git_stager = require('vgit.git.git_stager')
local git_status = require('vgit.git.git_status')
local git_conflict = require('vgit.git.git_conflict')
local Model = Object:extend()
function Model:constructor(opts)
return {
state = {
id = nil,
diffs = {},
entries = nil,
reponame = nil,
list_entries = {},
layout_type = opts.layout_type or 'unified',
},
}
end
function Model:reset()
self.state = {
id = nil,
diffs = {},
entries = nil,
reponame = nil,
list_entries = {},
layout_type = self.state.layout_type,
}
end
function Model:get_layout_type()
return self.state.layout_type
end
function Model:partition_status(statuses)
local changed_files = {}
local staged_files = {}
local unmerged_files = {}
utils.list.each(statuses, function(status)
if status:is_unmerged() then
local id = utils.math.uuid()
local data = { id = id, status = status, type = 'unmerged' }
self.state.list_entries[id] = data
table.insert(unmerged_files, data)
-- If something is unmerged it cannot be untracked or staged
return
end
if status:is_staged() then
local id = utils.math.uuid()
local data = { id = id, status = status, type = 'staged' }
self.state.list_entries[id] = data
table.insert(staged_files, data)
end
if status:is_unstaged() then
local id = utils.math.uuid()
local data = { id = id, status = status, type = 'unstaged' }
self.state.list_entries[id] = data
table.insert(changed_files, data)
end
end)
return changed_files, staged_files, unmerged_files
end
function Model:fetch()
self:reset()
if not git_repo.exists() then return nil, { 'Project has no .git folder' } end
loop.free_textlock()
local reponame = git_repo.discover()
local statuses, err = git_status.ls(reponame)
if err then return nil, err end
local changed_files, staged_files, unmerged_files = self:partition_status(statuses)
local entries = {}
if #unmerged_files ~= 0 then
entries[#entries + 1] = {
title = 'Merge Changes',
entries = unmerged_files
}
end
if #staged_files ~= 0 then
entries[#entries + 1] = {
title = 'Staged Changes',
entries = staged_files
}
end
if #changed_files ~= 0 then
entries[#entries + 1] = {
title = 'Changes',
entries = changed_files
}
end
self.state.entries = entries
self.state.reponame = reponame
return self.state.entries
end
function Model:set_entry_id(id)
self.state.id = id
end
function Model:get_entry(id)
if id then self.state.id = id end
return self.state.list_entries[self.state.id]
end
function Model:get_entries()
return self.state.entries
end
function Model:get_filename()
local entry, err = self:get_entry()
if err then return nil, err end
if not entry then return nil, { 'entry not found' } end
return entry.status.filename
end
function Model:get_filepath()
local reponame = self.state.reponame
local filename = self:get_filename()
if not filename then return nil, { 'entry not found' } end
filename = fs.make_relative(reponame, filename)
filename = string.format('%s/%s', reponame, filename)
return filename
end
function Model:get_filetype()
local entry, err = self:get_entry()
if err then return nil, err end
if not entry then return nil, { 'entry not found' } end
return entry.status.filetype
end
function Model:conflict_status()
return git_conflict.status(self.state.reponame)
end
function Model:get_lines(status, type)
local filename = status.filename
local reponame = self.state.reponame
if status:has('D ') then return git_show.lines(reponame, filename, 'HEAD') end
if type == 'staged' or status:has(' D') and not status:has_both('MD') then
return git_show.lines(reponame, filename)
end
if status:has('MD') then return git_show.lines(reponame, filename, 'HEAD^1') end
if type == 'unmerged' then return fs.read_file(self:get_filepath()) end
return fs.read_file(self:get_filepath())
end
function Model:get_hunks(status, type, lines)
local filename = status.filename
local reponame = self.state.reponame
if status:has_both('??') then return git_hunks.custom(lines, { untracked = true }) end
if type == 'staged' then return git_hunks.list(reponame, { filename = filename, staged = true }) end
if type == 'unstaged' then return git_hunks.list(reponame, { filename = filename }) end
return {}
end
function Model:get_diff()
local entry, err = self:get_entry()
if err then return nil, err end
if not entry then return nil, { 'entry not found' } end
local id = entry.id
local type = entry.type
local status = entry.status
if not status then return nil, { 'No status found in entry' } end
local cache_key = string.format('%s-%s-%s', id, type, status.id)
if self.state.diffs[cache_key] then return self.state.diffs[cache_key] end
local lines, lines_err = self:get_lines(status, type)
if lines_err then return nil, lines_err end
local layout_type = self:get_layout_type()
if type == 'unmerged' then
local conflicts = git_conflict.parse(lines)
local diff = Diff():generate(nil, lines, layout_type, { conflicts = conflicts })
self.state.diffs[cache_key] = diff
return diff
end
local hunks, hunks_err = self:get_hunks(status, type, lines)
if hunks_err then return nil, hunks_err end
loop.free_textlock()
local is_deleted = status:has_either('DD') and not status:has_both('MD')
local diff = Diff():generate(hunks, lines, layout_type, { is_deleted = is_deleted })
self.state.diffs[cache_key] = diff
return diff
end
function Model:stage_hunk(filename, hunk)
-- Use absolute path to avoid issues when cwd differs from repo root
local filepath = self.state.reponame .. '/' .. filename
local git_file = GitFile(filepath)
if not git_file:is_tracked() then return git_file:stage() end
local file, err = git_file:status()
if err then return nil, err end
if file:has('D ') or file:has(' D') then return git_file:stage() end
return git_file:stage_hunk(hunk)
end
function Model:unstage_hunk(filename, hunk)
-- Use absolute path to avoid issues when cwd differs from repo root
local filepath = self.state.reponame .. '/' .. filename
local git_file = GitFile(filepath)
if not git_file:is_tracked() then return git_file:unstage() end
local file, err = git_file:status()
if err then return nil, err end
if file:has('D ') or file:has(' D') then return git_file:unstage() end
return git_file:unstage_hunk(hunk)
end
function Model:reset_hunk(filename, hunk)
local filepath = self.state.reponame .. '/' .. filename
local git_file = GitFile(filepath)
return git_file:reset_hunk(hunk)
end
function Model:stage_file(filename)
return git_stager.stage(self.state.reponame, filename)
end
function Model:unstage_file(filename)
return git_stager.unstage(self.state.reponame, filename)
end
function Model:reset_file(filename)
local reponame = self.state.reponame
if git_repo.has(reponame, filename) then return git_repo.reset(reponame, filename) end
return git_repo.clean(reponame, filename)
end
function Model:stage_all()
return git_stager.stage(self.state.reponame)
end
function Model:unstage_all()
return git_stager.unstage(self.state.reponame)
end
function Model:reset_all()
local reponame = self.state.reponame
local _, reset_err = git_repo.reset(reponame)
if reset_err then return nil, reset_err end
return git_repo.clean(reponame)
end
return Model