Skip to content

Commit e789cef

Browse files
committed
fix: #1237
1 parent 7010000 commit e789cef

File tree

6 files changed

+75
-31
lines changed

6 files changed

+75
-31
lines changed

lua/gitsigns/async.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,11 @@ function Task:_finish(err, result)
182182
self._result = result
183183
threads[self._thread] = nil
184184
for _, cb in pairs(self._callbacks) do
185-
-- Needs to be pcall as step() (who calls this function) cannot error
186-
pcall(cb, err, result)
185+
-- Run the callbacks in the main loop so we can drain the callbacks
186+
-- and not interrupt step()
187+
vim.schedule(function()
188+
cb(err, result)
189+
end)
187190
end
188191
end
189192

@@ -399,7 +402,7 @@ end
399402
--- @generic F: function
400403
--- @param argc integer
401404
--- @param func F
402-
--- @return F
405+
--- @return fun(...): Gitsigns.async.Task
403406
function M.create(argc, func)
404407
assert(type(argc) == 'number')
405408
assert(type(func) == 'function')
@@ -409,7 +412,13 @@ function M.create(argc, func)
409412
return function(...)
410413
local task = Task._new(func)
411414

412-
local callback = argc and select(argc + 1, ...) or nil
415+
local callback = argc and select(argc + 1, ...) or function(err)
416+
-- Default continuation is to raise any errors
417+
if err then
418+
error(task:traceback(err))
419+
end
420+
end
421+
413422
if callback and type(callback) == 'function' then
414423
task:await(callback)
415424
end

lua/gitsigns/attach.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ local function parse_git_path(name)
4444
rel_path = tail:match('^[^/]+/(.*)')
4545
end
4646

47-
dprintf("%s buffer for file '%s' from path '%s' on commit '%s'", plugin, rel_path, file, commit)
47+
dprintf("%s buffer for file '%s' from path '%s' on commit '%s'", plugin, rel_path, name, commit)
4848
return rel_path, commit, gitdir
4949
end
5050

@@ -155,10 +155,16 @@ local function get_buf_context(bufnr)
155155
end
156156

157157
local gitdir_oap, toplevel_oap = on_attach_pre(bufnr)
158+
local gitdir = gitdir_oap or gitdir_from_bufname
159+
160+
if rel_path and gitdir then
161+
local toplevel = toplevel_oap or util.dirname(gitdir)
162+
file = util.joinpath(toplevel, rel_path)
163+
end
158164

159165
return {
160-
file = rel_path or file,
161-
gitdir = gitdir_oap or gitdir_from_bufname,
166+
file = file,
167+
gitdir = gitdir,
162168
toplevel = toplevel_oap,
163169
-- Stage buffers always compare against the common ancestor (':1')
164170
-- :0: index

lua/gitsigns/blame.lua

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ end
153153
--- @param win integer
154154
--- @param revision? string
155155
--- @param parent? boolean
156-
local function reblame(blame, win, revision, parent)
156+
local reblame = async.create(4, function(blame, win, revision, parent)
157157
local blm_win = api.nvim_get_current_win()
158158
local lnum = unpack(api.nvim_win_get_cursor(blm_win))
159159
local sha = blame[lnum].commit.sha
@@ -166,21 +166,17 @@ local function reblame(blame, win, revision, parent)
166166

167167
vim.cmd.quit()
168168
api.nvim_set_current_win(win)
169+
local bufnr = api.nvim_win_get_buf(win)
169170

170-
require('gitsigns').show(
171-
sha,
172-
vim.schedule_wrap(function()
173-
local bufnr = api.nvim_get_current_buf()
174-
local ok = vim.wait(1000, function()
175-
return cache[bufnr] ~= nil
176-
end)
177-
if not ok then
178-
error('Timeout waiting for attach')
179-
end
180-
async.arun(M.blame)
181-
end)
182-
)
183-
end
171+
async.await(require('gitsigns.diffthis').show(bufnr, sha))
172+
local ok = vim.wait(1000, function()
173+
return cache[bufnr] ~= nil
174+
end)
175+
if not ok then
176+
error('Timeout waiting for attach')
177+
end
178+
M.blame()
179+
end)
184180

185181
--- @param win integer
186182
--- @param open 'vsplit'|'tabnew'

lua/gitsigns/debug/log.lua

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local uv = vim.uv or vim.loop
33
local start_time = uv.hrtime()
44

55
--- @class Gitsigns.log
6-
--- @field private messages [number, string, string, string][]
6+
--- @field private messages [number, string, string, thread?, string][]
77
local M = {
88
debug_mode = false,
99
verbose = false,
@@ -94,7 +94,8 @@ local function cprint(kind, lvl, ...)
9494
if ctx.bufnr then
9595
ctx1 = string.format('%s(%s)', ctx1, ctx.bufnr)
9696
end
97-
table.insert(M.messages, { time, kind, ctx1, msg })
97+
local thread = coroutine.running()
98+
table.insert(M.messages, { time, kind, ctx1, thread, msg })
9899
end
99100

100101
function M.dprint(...)
@@ -131,7 +132,8 @@ local function eprint(msg, level)
131132
local info = debug.getinfo(level + 2, 'Sl')
132133
local ctx = info and string.format('%s<%d>', info.short_src, info.currentline) or '???'
133134
local time = (uv.hrtime() - start_time) / 1e6
134-
table.insert(M.messages, { time, 'error', ctx, debug.traceback(msg) })
135+
local thread = coroutine.running()
136+
table.insert(M.messages, { time, 'error', ctx, thread, debug.traceback(msg) })
135137
if M.debug_mode then
136138
error(msg, 3)
137139
end
@@ -167,18 +169,39 @@ function M.clear()
167169
M.messages = {}
168170
end
169171

170-
--- @param m [number, string, string, string]
172+
--- @param thread thread
173+
--- @return string
174+
local function fmt_thread(thread)
175+
local threads = _G.tostring(thread)
176+
local thread1 = threads:sub(#threads - 1)
177+
return '<t:' .. thread1 .. '>'
178+
end
179+
180+
--- @param m [number, string, string, thread, string]
171181
--- @return [string,string][]
172182
local function build_msg(m)
173-
local time, kind, ctx, msg = m[1], m[2], m[3], m[4]
183+
local time, kind, ctx, thread, msg = m[1], m[2], m[3], m[4], m[5]
174184
local hl = sev_to_hl[kind]
175-
return {
185+
186+
local r = {
176187
{ string.format('%.2f ', time), 'Comment' },
188+
}
189+
190+
if thread then
191+
vim.list_extend(r, {
192+
{ fmt_thread(thread), 'Comment' },
193+
{ ' ' },
194+
})
195+
end
196+
197+
vim.list_extend(r, {
177198
{ kind:upper():sub(1, 1), hl },
178199
{ string.format(' %s:', ctx), 'Tag' },
179200
{ ' ' },
180-
{ msg },
181-
}
201+
{ msg }
202+
})
203+
204+
return r
182205
end
183206

184207
function M.show()

lua/gitsigns/system/compat.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ end
275275
--- @param on_exit? fun(out: vim.SystemCompleted)
276276
--- @return vim.SystemObj
277277
local function system(cmd, opts, on_exit)
278-
local __FUNC__ = 'run_job'
278+
local __FUNC__ = 'system'
279279
vim.validate({
280280
cmd = { cmd, 'table' },
281281
opts = { opts, 'table', true },

lua/gitsigns/util.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ function M.dirname(file)
2424
return file:match(string.format('^(.+)%s[^%s]+', M.path_sep, M.path_sep))
2525
end
2626

27+
---@param ... string
28+
---@return string
29+
function M.joinpath(...)
30+
local path = table.concat({ ... }, '/')
31+
if not is_unix then
32+
path = path:gsub('\\', '/')
33+
end
34+
return (path:gsub('//+', '/'))
35+
end
36+
2737
--- @param path string
2838
--- @return string[]
2939
function M.file_lines(path)

0 commit comments

Comments
 (0)