Skip to content

Commit b093147

Browse files
committed
fix(snapshot): use correct gitdir path and --cached diff
opencode stores the snapshot gitdir at {project_id}/{sha1(worktree)}, not {project_id} directly. All git ops were hitting the wrong repo. Also switch diff/patch commands to --cached (matching opencode's impl) and use --git-dir/--work-tree instead of -C to avoid ambiguity. This should fix #384
1 parent fc8440d commit b093147

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

lua/opencode/config_file.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ M.get_opencode_project = Promise.async(function()
4040
return result --[[@as OpencodeProject|nil]]
4141
end)
4242

43+
---Compute SHA1 of a string (used to match opencode's Hash.fast for worktree path)
44+
---@param str string
45+
---@return string|nil
46+
local function sha1(str)
47+
local result = vim.system({ 'sh', '-c', 'printf "%s" "$1" | sha1sum', '_', str }):wait()
48+
if result and result.code == 0 then
49+
return vim.trim(result.stdout):match('^(%x+)')
50+
end
51+
end
52+
4353
---Get the snapshot storage path for the current workspace
4454
---@type fun(): Promise<string>
4555
M.get_workspace_snapshot_path = Promise.async(function()
@@ -48,7 +58,12 @@ M.get_workspace_snapshot_path = Promise.async(function()
4858
return ''
4959
end
5060
local home = vim.uv.os_homedir()
51-
return home .. '/.local/share/opencode/snapshot/' .. project.id
61+
local cwd = vim.fn.getcwd()
62+
local worktree_hash = sha1(cwd)
63+
if not worktree_hash then
64+
return ''
65+
end
66+
return home .. '/.local/share/opencode/snapshot/' .. project.id .. '/' .. worktree_hash
5267
end)
5368

5469
local _providers_render_callback = false

lua/opencode/git_review.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ local function snapshot_git(cmd_args, opts)
1717
vim.notify('No snapshot path for the active session.')
1818
return nil, nil
1919
end
20-
local args = { 'git', '-C', M.__snapshot_path }
20+
local cwd = vim.fn.getcwd()
21+
local args = { 'git', '--git-dir', M.__snapshot_path, '--work-tree', cwd }
2122
vim.list_extend(args, cmd_args)
22-
local result = vim.system(args, opts or {}):wait()
23+
local result = vim.system(args, opts or { cwd = cwd }):wait()
2324
if result and result.code == 0 then
2425
return vim.trim(result.stdout), result.stderr
2526
else
@@ -286,7 +287,9 @@ M.revert_selected_file = require_git_project(function(ref)
286287
end)
287288

288289
M.revert_all = require_git_project(function(ref)
290+
vim.print('⭕ ❱ git_review.lua:288 ❱ ƒ(anonymous) ❱ ref =', ref)
289291
M.__current_ref = ref or M.get_first_snapshot()
292+
vim.print('⭕ ❱ git_review.lua:289 ❱ ƒ(M.__current_ref) ❱ M.__current_ref =', M.__current_ref)
290293

291294
local files = get_changed_files()
292295

lua/opencode/snapshot.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ local session = require('opencode.session')
1212
---@return string|nil, string|nil
1313
local function snapshot_git(cmd_args, opts)
1414
local snapshot_dir = config_file.get_workspace_snapshot_path():wait()
15-
if not snapshot_dir then
15+
if not snapshot_dir or snapshot_dir == '' then
1616
vim.notify('No snapshot path for the active session.')
1717
return nil, nil
1818
end
19-
local args = { 'git', '-C', snapshot_dir }
19+
local cwd = vim.fn.getcwd()
20+
local args = { 'git', '--git-dir', snapshot_dir, '--work-tree', cwd }
2021
vim.list_extend(args, cmd_args)
21-
local result = vim.system(args, opts or {}):wait()
22+
local result = vim.system(args, opts or { cwd = cwd }):wait()
2223
if result and result.code == 0 then
2324
return vim.trim(result.stdout), nil
2425
else
@@ -133,7 +134,7 @@ function M.patch(hash)
133134
vim.notify('Failed to add files: ' .. add_err .. _, vim.log.levels.WARN)
134135
end
135136

136-
local files_output, diff_err = snapshot_git({ 'diff', '--name-only', hash, '--', '.' })
137+
local files_output, diff_err = snapshot_git({ 'diff', '--cached', '--no-ext-diff', '--name-only', hash, '--', '.' })
137138
if not files_output then
138139
vim.notify('Failed to get diff: ' .. (diff_err or 'unknown error'), vim.log.levels.ERROR)
139140
return nil
@@ -160,7 +161,7 @@ function M.diff(hash)
160161
return nil
161162
end
162163

163-
local result, err = snapshot_git({ 'diff', hash, '--', '.' })
164+
local result, err = snapshot_git({ 'diff', '--cached', '--no-ext-diff', hash, '--', '.' })
164165
if not result then
165166
vim.notify('Failed to get diff: ' .. (err or 'unknown error'), vim.log.levels.ERROR)
166167
return nil

0 commit comments

Comments
 (0)