Adds git support for optolith

This commit is contained in:
Michel 2025-01-03 14:19:18 +01:00
parent 2ea0877394
commit 576eba8e4e
5 changed files with 82 additions and 47 deletions

View file

@ -1,4 +1,5 @@
local nio = require("nio")
local git = require("functions.git")
local function startswith(string, start)
return string:sub(1, #start) == start
@ -29,8 +30,7 @@ vim.api.nvim_create_user_command("Optolith", function()
end
local absOptolithDirectory = vim.fs.normalize(OPTOLITH_DIRECTORY)
local directoryExists = vim.fn.isdirectory(absOptolithDirectory)
if directoryExists == 0 then
if vim.fn.isdirectory(absOptolithDirectory) == 0 then
vim.notify("Can't find folder. Please start the application once.\nFolder: " .. OPTOLITH_DIRECTORY, "error", {
title = "Optolith",
})
@ -52,6 +52,7 @@ vim.api.nvim_create_user_command("Optolith", function()
vim.notify("Swap file already exists. To prevent data loss, please restore the swap file.", "error", {
title = "Optolith",
})
return
end
if heroesFileExists then
local mvCommand = os.execute(string.format('mv "%s" "%s.swp"', heroesPath, heroesPath))
@ -72,6 +73,8 @@ vim.api.nvim_create_user_command("Optolith", function()
return
end
-- If the file is just one character, its not a valid heroes.json file.
-- So we need to create a valid file.
local file = table.concat(vim.fn.readfile(currentHeroFile))
local fileData = vim.fn.json_decode(file)
if fileData["id"] ~= nil then
@ -91,5 +94,7 @@ vim.api.nvim_create_user_command("Optolith", function()
if swapFileExists then
os.execute(string.format('mv "%s.swp" "%s"', heroesPath, heroesPath))
end
git.commitFile(currentHeroFile)
end)
end, {})