If file is managed by git then rename using git-mv instead of filesystem mv
parent
081b0c13cf
commit
c7629620a5
|
|
@ -39,13 +39,49 @@ function M.lspSupportsRenaming()
|
||||||
return false
|
return false
|
||||||
end
|
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 oldFilePath string
|
||||||
---@param newFilePath string
|
---@param newFilePath string
|
||||||
function M.moveFile(oldFilePath, newFilePath)
|
function M.moveFile(oldFilePath, newFilePath)
|
||||||
local u = require("genghis.support.utils")
|
local u = require("genghis.support.utils")
|
||||||
|
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)
|
local renamed, _ = vim.uv.fs_rename(oldFilePath, newFilePath)
|
||||||
if renamed then return true end
|
if renamed then return true end
|
||||||
|
end
|
||||||
|
|
||||||
---try `fs_copyfile` to support moving across partitions
|
---try `fs_copyfile` to support moving across partitions
|
||||||
local copied, copiedError = vim.uv.fs_copyfile(oldFilePath, newFilePath)
|
local copied, copiedError = vim.uv.fs_copyfile(oldFilePath, newFilePath)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue