If file is managed by git then rename using git-mv instead of filesystem mv

main
Bryan 2024-11-01 22:32:02 -06:00
parent 081b0c13cf
commit c7629620a5
1 changed files with 39 additions and 3 deletions

View File

@ -39,13 +39,49 @@ function M.lspSupportsRenaming()
return false
end
---@param filePath string
local function isGitManagedFile(filePath)
local obj = vim.system({ "git", "ls-files", "--error-unmatch", filePath }, { text = true })
:wait()
if obj.code == 0 then
return true
else
return false
end
end
---@param oldFilePath string
---@param newFilePath string
local function git_mv(oldFilePath, newFilePath)
local obj = vim.system({ "git", "mv", oldFilePath, newFilePath }, { text = true }):wait()
if obj.code == 0 then
return true, obj.stderr
else
return false, obj.stderr
end
end
---@param oldFilePath string
---@param newFilePath string
function M.moveFile(oldFilePath, newFilePath)
local u = require("genghis.support.utils")
local renamed, _ = vim.uv.fs_rename(oldFilePath, newFilePath)
if renamed then return true end
if isGitManagedFile(oldFilePath) then
local renamed, git_mv_error = git_mv(oldFilePath, newFilePath)
if renamed then
return true
else
u.notify(
("File managed by git.\nFailed to rename using git mv %q:\n%q\nAttempting to rename using fs."):format(
oldFilePath,
git_mv_error
),
"error"
)
end
else
local renamed, _ = vim.uv.fs_rename(oldFilePath, newFilePath)
if renamed then return true end
end
---try `fs_copyfile` to support moving across partitions
local copied, copiedError = vim.uv.fs_copyfile(oldFilePath, newFilePath)