From f2c3c6f715f0e1bccdba515739f55e15d4d76ee0 Mon Sep 17 00:00:00 2001 From: chomosuke Date: Mon, 28 Nov 2022 15:26:31 +1100 Subject: [PATCH 1/4] fixed git rename not showing up for the renamed file --- lua/nvim-tree/git/runner.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree/git/runner.lua b/lua/nvim-tree/git/runner.lua index 686b5f80bd5..1ca5f5823c9 100644 --- a/lua/nvim-tree/git/runner.lua +++ b/lua/nvim-tree/git/runner.lua @@ -6,8 +6,17 @@ Runner.__index = Runner function Runner:_parse_status_output(line) local status = line:sub(1, 2) + local path = line:sub(4, -2) + if utils.str_find(status, "R") then + -- rename have format of "from -> to" + -- we just what the "to" part + local _, j = path:find " -> " + -- Note: The edge case where " -> " is a part of a file name is not addressed + path = path:sub(j + 1, -1) + end + -- removing `"` when git is returning special file status containing spaces - local path = line:sub(4, -2):gsub('^"', ""):gsub('"$', "") + path = path:gsub('^"', ""):gsub('"$', "") -- replacing slashes if on windows if vim.fn.has "win32" == 1 then path = path:gsub("/", "\\") From 46dc0313f6a78304e127dcefe2b0103bd84e10b3 Mon Sep 17 00:00:00 2001 From: chomosuke Date: Mon, 28 Nov 2022 18:13:48 +1100 Subject: [PATCH 2/4] considered " -> " being a part of the filename Fixed -> pattern to escape - Fixed "\"" and "\\" in filename --- lua/nvim-tree/git/runner.lua | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree/git/runner.lua b/lua/nvim-tree/git/runner.lua index 1ca5f5823c9..bdffa149191 100644 --- a/lua/nvim-tree/git/runner.lua +++ b/lua/nvim-tree/git/runner.lua @@ -10,13 +10,23 @@ function Runner:_parse_status_output(line) if utils.str_find(status, "R") then -- rename have format of "from -> to" -- we just what the "to" part - local _, j = path:find " -> " - -- Note: The edge case where " -> " is a part of a file name is not addressed - path = path:sub(j + 1, -1) + if path:sub(1, 1) == '"' then + -- incase " -> " is a part of the file name + local _, j = path:find '" %-> "' + path = path:sub(j, -1) + else + local _, j = path:find " %-> " + path = path:sub(j + 1, -1) + end end -- removing `"` when git is returning special file status containing spaces - path = path:gsub('^"', ""):gsub('"$', "") + if path:sub(1, 1) == '"' then + path = path:sub(2, -2) + path = path:gsub('\\"', '"') + path = path:gsub("\\\\", "\\") + end + -- replacing slashes if on windows if vim.fn.has "win32" == 1 then path = path:gsub("/", "\\") @@ -131,6 +141,7 @@ function Runner:_wait() local function is_done() return self.rc ~= nil end + while not vim.wait(30, is_done) do end end From 05dea4740b48ec51068aa047f7b41b97c6073d12 Mon Sep 17 00:00:00 2001 From: chomosuke Date: Tue, 29 Nov 2022 18:46:15 +1100 Subject: [PATCH 3/4] using string.find(, , true) to match plain -> --- lua/nvim-tree/git/runner.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/git/runner.lua b/lua/nvim-tree/git/runner.lua index bdffa149191..9b65ca530c0 100644 --- a/lua/nvim-tree/git/runner.lua +++ b/lua/nvim-tree/git/runner.lua @@ -12,10 +12,10 @@ function Runner:_parse_status_output(line) -- we just what the "to" part if path:sub(1, 1) == '"' then -- incase " -> " is a part of the file name - local _, j = path:find '" %-> "' + local _, j = path:find('" -> "', nil, true) path = path:sub(j, -1) else - local _, j = path:find " %-> " + local _, j = path:find(" -> ", nil, true) path = path:sub(j + 1, -1) end end From 2bb288e7d1735e327afd6bf4ce1a1e75f591b6a7 Mon Sep 17 00:00:00 2001 From: chomosuke Date: Tue, 29 Nov 2022 19:14:02 +1100 Subject: [PATCH 4/4] Using -z and removed unnecessary logic --- lua/nvim-tree/git/runner.lua | 44 +++++++++++++++--------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/lua/nvim-tree/git/runner.lua b/lua/nvim-tree/git/runner.lua index 9b65ca530c0..a03c72e4bca 100644 --- a/lua/nvim-tree/git/runner.lua +++ b/lua/nvim-tree/git/runner.lua @@ -4,29 +4,7 @@ local utils = require "nvim-tree.utils" local Runner = {} Runner.__index = Runner -function Runner:_parse_status_output(line) - local status = line:sub(1, 2) - local path = line:sub(4, -2) - if utils.str_find(status, "R") then - -- rename have format of "from -> to" - -- we just what the "to" part - if path:sub(1, 1) == '"' then - -- incase " -> " is a part of the file name - local _, j = path:find('" -> "', nil, true) - path = path:sub(j, -1) - else - local _, j = path:find(" -> ", nil, true) - path = path:sub(j + 1, -1) - end - end - - -- removing `"` when git is returning special file status containing spaces - if path:sub(1, 1) == '"' then - path = path:sub(2, -2) - path = path:gsub('\\"', '"') - path = path:gsub("\\\\", "\\") - end - +function Runner:_parse_status_output(status, path) -- replacing slashes if on windows if vim.fn.has "win32" == 1 then path = path:gsub("/", "\\") @@ -34,15 +12,26 @@ function Runner:_parse_status_output(line) if #status > 0 and #path > 0 then self.output[utils.path_remove_trailing(utils.path_join { self.project_root, path })] = status end - return #line end function Runner:_handle_incoming_data(prev_output, incoming) if incoming and utils.str_find(incoming, "\n") then local prev = prev_output .. incoming local i = 1 + local skip_next_line = false for line in prev:gmatch "[^\n]*\n" do - i = i + self:_parse_status_output(line) + if skip_next_line then + skip_next_line = false + else + local status = line:sub(1, 2) + local path = line:sub(4, -2) + if utils.str_find(status, "R") then + -- skip next line if it is a rename entry + skip_next_line = true + end + self:_parse_status_output(status, path) + end + i = i + #line end return prev:sub(i, -1) @@ -63,7 +52,7 @@ function Runner:_getopts(stdout_handle, stderr_handle) local untracked = self.list_untracked and "-u" or nil local ignored = (self.list_untracked and self.list_ignored) and "--ignored=matching" or "--ignored=no" return { - args = { "--no-optional-locks", "status", "--porcelain=v1", ignored, untracked, self.path }, + args = { "--no-optional-locks", "status", "--porcelain=v1", "-z", ignored, untracked, self.path }, cwd = self.project_root, stdio = { nil, stdout_handle, stderr_handle }, } @@ -125,6 +114,9 @@ function Runner:_run_git_job() if err then return end + if data then + data = data:gsub("%z", "\n") + end self:_log_raw_output(data) output_leftover = self:_handle_incoming_data(output_leftover, data) end