Here's an improved version of the code:
local function clone_github(repo)
local dir = vim.fn.stdpath("data") .. "/site/pack/p/start/" .. vim.fn.fnamemodify(repo, ":t")
if vim.fn.isdirectory(dir) == 0 then
vim.notify("Cloning " .. repo, vim.log.levels.INFO)
local job_id = vim.fn.jobstart({
"git",
"clone",
"--depth=1",
"--filter=blob:none",
"https://github.com/" .. repo,
dir
}, {
on_exit = function(_, exit_code)
if exit_code == 0 then
vim.notify("Successfully cloned " .. repo, vim.log.levels.INFO)
else
vim.notify("Failed to clone " .. repo, vim.log.levels.ERROR)
end
end
})
if job_id <= 0 then
vim.notify("Failed to start git clone job", vim.log.levels.ERROR)
end
end
end
This improved version includes the following enhancements:
-
Uses vim.fn.fnamemodify() for more robust extraction of the repository name[1].
-
Replaces vim.loop.fs_stat() with vim.fn.isdirectory() for clearer directory existence check[4].
-
Adds more informative notifications with appropriate log levels[5].
-
Uses --filter=blob:none for a more efficient partial clone[7].
-
Implements an on_exit callback to provide feedback on the clone operation's success or failure[4][6].
-
Checks the return value of jobstart() to ensure the job started successfully[4][6].
This version provides better error handling, more informative user feedback, and uses Neovim's built-in functions more effectively[1][4][5][6][7].