-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathsnapshot.lua
More file actions
276 lines (241 loc) · 8.3 KB
/
snapshot.lua
File metadata and controls
276 lines (241 loc) · 8.3 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
-- This file is a port of the snapshot management logic from the original OpenCode
---@see https://github.com/sst/opencode/blob/dev/packages/opencode/src/snapshot/index.ts
local M = {}
local state = require('opencode.state')
local util = require('opencode.util')
local config_file = require('opencode.config_file')
local session = require('opencode.session')
---@param cmd_args string[]
---@param opts? vim.SystemOpts
---@return string|nil, string|nil
local function snapshot_git(cmd_args, opts)
local snapshot_dir = config_file.get_workspace_snapshot_path():wait()
if not snapshot_dir then
vim.notify('No snapshot path for the active session.')
return nil, nil
end
local args = { 'git', '-C', snapshot_dir }
vim.list_extend(args, cmd_args)
local result = vim.system(args, opts or {}):wait()
if result and result.code == 0 then
return vim.trim(result.stdout), nil
else
return nil, result and result.stderr or nil
end
end
local function write_to_temp_file(content)
local temp_file = vim.fn.tempname()
local f = io.open(temp_file, 'w')
if not f then
vim.notify('Failed to open temp file: ' .. temp_file)
return nil
end
f:write(content)
f:close()
return temp_file
end
function M.track()
if not state.active_session then
vim.notify('No active session', vim.log.levels.ERROR)
return nil
end
local snapshot_dir = config_file.get_workspace_snapshot_path():wait()
if not snapshot_dir then
vim.notify('No snapshot path for the active session.')
return nil
end
local _, add_err = snapshot_git({ 'add', '.' })
if add_err then
vim.notify('Failed to add files: ' .. add_err, vim.log.levels.WARN)
end
local hash_output, write_tree_err = snapshot_git({ 'write-tree' })
if not hash_output then
vim.notify('Failed to write tree: ' .. (write_tree_err or 'unknown error'), vim.log.levels.ERROR)
return nil
end
return vim.trim(hash_output)
end
function M.create()
return M.track()
end
function M.save_restore_point(snapshot_id, from_snapshot_id, deleted_files)
if not state.active_session then
vim.notify('No active session', vim.log.levels.ERROR)
return nil
end
local cache_path = session.get_cache_path(state.active_session.id)
local patch_result = M.patch(snapshot_id)
local snapshot = {
id = snapshot_id,
from_snapshot_id = from_snapshot_id or nil,
files = patch_result and patch_result.files or {},
deleted_files = deleted_files or {},
created_at = os.time(),
}
local path = cache_path .. 'snapshots/'
if vim.fn.isdirectory(path) == 0 then
vim.fn.mkdir(path, 'p')
end
local snapshot_file = path .. snapshot_id .. '.json'
local ok, err = pcall(vim.fn.writefile, { vim.json.encode(snapshot) }, snapshot_file)
if not ok then
vim.notify('Failed to write restore point: ' .. err, vim.log.levels.ERROR)
return nil
end
state.event_manager:emit('custom.restore_point.created', { restore_point = snapshot })
return snapshot
end
---@return RestorePoint[]
function M.get_restore_points()
if not state.active_session then
state.session.reset_restore_points()
return {}
end
local cache_path = session.get_cache_path(state.active_session.id)
if not cache_path then
return {}
end
if state.restore_points and #state.restore_points > 0 then
return state.restore_points
end
local restore_points = util.read_json_dir(cache_path .. 'snapshots/') or {}
table.sort(restore_points, function(a, b)
return a.created_at > b.created_at
end)
state.session.set_restore_points(restore_points)
return state.restore_points
end
---@return OpencodeSnapshotPatch|nil
function M.patch(hash)
if not state.active_session then
vim.notify('No active session', vim.log.levels.ERROR)
return nil
end
local _, add_err = snapshot_git({ 'add', '.' })
if add_err then
vim.notify('Failed to add files: ' .. add_err .. _, vim.log.levels.WARN)
end
local files_output, diff_err = snapshot_git({ 'diff', '--name-only', hash, '--', '.' })
if not files_output then
vim.notify('Failed to get diff: ' .. (diff_err or 'unknown error'), vim.log.levels.ERROR)
return nil
end
local files = {}
local cwd = vim.fn.getcwd()
for line in files_output:gmatch('[^\r\n]+') do
local trimmed = vim.trim(line)
if trimmed ~= '' then
table.insert(files, cwd .. '/' .. trimmed)
end
end
return {
hash = hash,
files = files,
}
end
function M.diff(hash)
if not state.active_session then
vim.notify('No active session', vim.log.levels.ERROR)
return nil
end
local result, err = snapshot_git({ 'diff', hash, '--', '.' })
if not result then
vim.notify('Failed to get diff: ' .. (err or 'unknown error'), vim.log.levels.ERROR)
return nil
end
return vim.trim(result)
end
function M.diff_file(snapshot_id, file_path)
local relative_path = vim.fn.fnamemodify(file_path, ':.')
local file_at_snapshot = snapshot_git({ 'show', snapshot_id .. ':' .. relative_path })
local temp_file = write_to_temp_file(file_at_snapshot or '')
local file_type = vim.fn.fnamemodify(file_path, ':e')
return { left = file_path, right = temp_file, file_type = file_type }
end
function M.revert(snapshot_id)
local restore_point_id = M.create()
local patch_result = M.patch(snapshot_id)
if not patch_result then
vim.notify('Failed to revert snapshot: ' .. snapshot_id, vim.log.levels.ERROR)
return
end
local deleted_files = {}
for _, file in ipairs(patch_result.files) do
local relative_path = file:match('^' .. vim.fn.getcwd() .. '/?(.*)$')
local res, err = snapshot_git({ 'checkout', snapshot_id, '--', relative_path })
if not res then
vim.notify(
'file not found in history, deleting: ' .. file .. ' - ' .. (err or 'unknown error'),
vim.log.levels.WARN
)
vim.fn.delete(file)
table.insert(deleted_files, file)
end
vim.cmd('checktime')
end
M.save_restore_point(restore_point_id, snapshot_id, deleted_files)
return restore_point_id, deleted_files
end
---@param snapshot_id string
---@param file_path string
---@return string|nil, string[]
function M.revert_file(snapshot_id, file_path)
local restore_point_id = M.create()
local relative_path = vim.fn.fnamemodify(file_path, ':.')
local res, err = snapshot_git({ 'checkout', snapshot_id, '--', relative_path })
local deleted_files = {}
if not res then
vim.notify(
'file not found in history, deleting: ' .. file_path .. ' - ' .. (err or 'unknown error'),
vim.log.levels.WARN
)
vim.fn.delete(file_path)
table.insert(deleted_files, file_path)
end
vim.cmd('checktime')
M.save_restore_point(restore_point_id, snapshot_id, deleted_files)
return restore_point_id, deleted_files
end
---@param snapshot_id string
function M.restore(snapshot_id)
local read_tree_out, read_tree_err = snapshot_git({ 'read-tree', snapshot_id })
if not read_tree_out then
vim.notify('Failed to read-tree: ' .. (read_tree_err or 'unknown error'), vim.log.levels.ERROR)
return
end
local checkout_out, checkout_err = snapshot_git({ 'checkout-index', '-a', '-f' })
if not checkout_out then
vim.notify('Failed to checkout-index: ' .. (checkout_err or 'unknown error'), vim.log.levels.ERROR)
return
end
vim.notify('Restored snapshot: ' .. snapshot_id, vim.log.levels.INFO)
end
function M.restore_file(snapshot_id, file_path)
local read_tree_out, read_tree_err = snapshot_git({ 'read-tree', snapshot_id })
if not read_tree_out then
vim.notify('Failed to read-tree: ' .. (read_tree_err or 'unknown error'), vim.log.levels.ERROR)
return
end
local checkout_out, checkout_err = snapshot_git({ 'checkout-index', '-f', '--', file_path })
if not checkout_out then
vim.notify('Failed to checkout-index: ' .. (checkout_err or 'unknown error'), vim.log.levels.ERROR)
return
end
vim.notify('Restored file: ' .. file_path .. ' from snapshot: ' .. snapshot_id, vim.log.levels.INFO)
end
---@param from_snapshot_id string
---@return RestorePoint[]|nil
function M.get_restore_points_by_parent(from_snapshot_id)
local restore_points = M.get_restore_points()
restore_points = vim.tbl_filter(function(item)
return item.from_snapshot_id == from_snapshot_id
end, restore_points)
table.sort(restore_points, function(a, b)
return a.created_at > b.created_at
end)
if #restore_points == 0 then
return nil
end
return restore_points
end
return M