From 213a39f8161f80ce43e1a164a54fe796432422b8 Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Fri, 12 Sep 2025 17:28:46 +0200 Subject: [PATCH] fix: blame blocked on non-git files --- README.md | 3 ++- lua/line-blame.lua | 9 ++++++++- lua/monkey-alert.lua | 18 ++++++++++++++---- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4f6970b..a3e571f 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,5 @@ Git line blame by mail. ## 🚨 Known issues -- [ ] Error on non-git files. +- [x] Error on non-git files. +- [ ] Performance: the autocmd should not be processed on non-git files, to this date it is simply not displayed but a `$ git log` is run. diff --git a/lua/line-blame.lua b/lua/line-blame.lua index e0cf872..54ccb0f 100644 --- a/lua/line-blame.lua +++ b/lua/line-blame.lua @@ -16,8 +16,15 @@ end local lineBlame = function(input_file_path, line_number, input_comma_separated_mail_list, blame_text, blame_highlight, blame_position) + local git_exit_code = os.execute("git log >/dev/null 2>&1") + + if git_exit_code ~= 0 then + return nil + end + local handler = io.popen("git blame -L " .. line_number .. "," .. line_number .. " -e " .. input_file_path .. " | awk '{print $2}'") + if handler == nil then return nil end @@ -30,7 +37,7 @@ local lineBlame = function(input_file_path, line_number, input_comma_separated_m local mail = string.match(result, "%(<(.+)>") - if not mail then + if not mail or mail == nil or mail == "" then return nil end diff --git a/lua/monkey-alert.lua b/lua/monkey-alert.lua index 5cc2370..f541758 100644 --- a/lua/monkey-alert.lua +++ b/lua/monkey-alert.lua @@ -23,13 +23,23 @@ local function blameCurrentLine() "Monkey", vim.g.monkey_blame_position) end +local function updateLineBlame() + if vim.bo.buftype == nil or vim.bo.buftype == '' then + clearBlame() + blameCurrentLine() + end +end + local function enableOnLine() + -- vim.api.nvim_create_autocmd("BufNew", { + -- callback = function(event) + -- local status = vim.api.nvim_buf_attach(event.buf, false, { on_changedtick = updateLineBlame }) + -- -- status == false, for buffers that aren't bound to a file, because the buffer isn't loaded yet + -- end + -- }) vim.api.nvim_create_autocmd("CursorMoved", { callback = function() - if vim.bo.buftype == nil or vim.bo.buftype == '' then - clearBlame() - blameCurrentLine() - end + updateLineBlame() end, }) end