From 73d068a7be00bc0533c1d87cc863d4127fe20814 Mon Sep 17 00:00:00 2001 From: lschwahn Date: Fri, 28 Jan 2022 20:12:35 +0100 Subject: [PATCH] fix: gitsigns in fugitive buffer not visible --- lua/gitsigns.lua | 35 ++++++++++++++++++++++------------- teal/gitsigns.tl | 35 ++++++++++++++++++++++------------- test/unit_spec.lua | 13 +++++++++++++ types/types.d.tl | 2 ++ 4 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 test/unit_spec.lua diff --git a/lua/gitsigns.lua b/lua/gitsigns.lua index f4104e84..0e43117f 100644 --- a/lua/gitsigns.lua +++ b/lua/gitsigns.lua @@ -159,6 +159,23 @@ M.detach = function(bufnr, _keep_signs) cache:destroy(bufnr) end +local function parse_fugitive_uri(name) + local _, _, root_path, sub_module_path, commit, real_path = + name:find([[^fugitive://(.*)/%.git(.*/)/(%x-)/(.*)]]) + if root_path then + sub_module_path = sub_module_path:gsub("^/modules", "") + name = root_path .. sub_module_path .. real_path + end + return name, commit +end + +if _TEST then + local path, commit = parse_fugitive_uri( + 'fugitive:///home/path/to/project/.git//1b441b947c4bc9a59db428f229456619051dd133/subfolder/to/a/file.txt') + assert(path == '/home/path/to/project/subfolder/to/a/file.txt', string.format('GOT %s', path)) + assert(commit == '1b441b947c4bc9a59db428f229456619051dd133', string.format('GOT %s', commit)) +end + local function get_buf_path(bufnr) local file = uv.fs_realpath(api.nvim_buf_get_name(bufnr)) or @@ -168,19 +185,11 @@ local function get_buf_path(bufnr) end) if vim.startswith(file, 'fugitive://') and vim.wo.diff == false then - local orig_path = file - local _, _, root_path, sub_module_path, commit, real_path = - file:find([[^fugitive://(.*)/%.git(.*)/(%x-)/(.*)]]) - if root_path then - sub_module_path = sub_module_path:gsub("^/modules", "") - file = root_path .. sub_module_path .. real_path - file = uv.fs_realpath(file) - dprintf("Fugitive buffer for file '%s' from path '%s'", file, orig_path) - if file then - return file, commit - else - file = orig_path - end + local path, commit = parse_fugitive_uri(file) + dprintf("Fugitive buffer for file '%s' from path '%s'", path, file) + path = uv.fs_realpath(path) + if path then + return path, commit end end diff --git a/teal/gitsigns.tl b/teal/gitsigns.tl index c99f47ef..cb7b0be3 100644 --- a/teal/gitsigns.tl +++ b/teal/gitsigns.tl @@ -159,6 +159,23 @@ M.detach = function(bufnr: integer, _keep_signs: boolean) cache:destroy(bufnr) end +local function parse_fugitive_uri(name: string): string, string + local _, _, root_path, sub_module_path, commit, real_path = + name:find([[^fugitive://(.*)/%.git(.*/)/(%x-)/(.*)]]) + if root_path then + sub_module_path = sub_module_path:gsub("^/modules", "") + name = root_path .. sub_module_path .. real_path + end + return name, commit +end + +if _TEST then + local path, commit = parse_fugitive_uri( + 'fugitive:///home/path/to/project/.git//1b441b947c4bc9a59db428f229456619051dd133/subfolder/to/a/file.txt') + assert(path == '/home/path/to/project/subfolder/to/a/file.txt', string.format('GOT %s', path)) + assert(commit == '1b441b947c4bc9a59db428f229456619051dd133', string.format('GOT %s', commit)) +end + local function get_buf_path(bufnr: integer): string, string local file = uv.fs_realpath(api.nvim_buf_get_name(bufnr)) @@ -168,19 +185,11 @@ local function get_buf_path(bufnr: integer): string, string end) if vim.startswith(file, 'fugitive://') and vim.wo.diff == false then - local orig_path = file - local _,_, root_path, sub_module_path, commit, real_path = - file:find([[^fugitive://(.*)/%.git(.*)/(%x-)/(.*)]]) - if root_path then - sub_module_path = sub_module_path:gsub("^/modules", "") - file = root_path .. sub_module_path .. real_path - file = uv.fs_realpath(file) - dprintf("Fugitive buffer for file '%s' from path '%s'", file, orig_path) - if file then - return file, commit - else - file = orig_path - end + local path, commit = parse_fugitive_uri(file) + dprintf("Fugitive buffer for file '%s' from path '%s'", path, file) + path = uv.fs_realpath(path) + if path then + return path, commit end end diff --git a/test/unit_spec.lua b/test/unit_spec.lua new file mode 100644 index 00000000..c546ec74 --- /dev/null +++ b/test/unit_spec.lua @@ -0,0 +1,13 @@ +local helpers = require('test.gs_helpers') +local exec_lua = helpers.exec_lua +local setup_gitsigns = helpers.setup_gitsigns +local clear = helpers.clear + +describe('unit', function() + it('passes all _TEST blocks', function() + clear() + exec_lua('package.path = ...', package.path) + exec_lua('_TEST = true') + setup_gitsigns() + end) +end) diff --git a/types/types.d.tl b/types/types.d.tl index d6ddbc58..51dc5313 100644 --- a/types/types.d.tl +++ b/types/types.d.tl @@ -1,5 +1,7 @@ global _: any +global _TEST: boolean + global loadstring: function(string): (function(): any) global unpack: function({T}, number, number): T...