Adds git support for optolith
This commit is contained in:
parent
2ea0877394
commit
576eba8e4e
5 changed files with 82 additions and 47 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
|
package.path = package.path .. ";./lua"
|
||||||
|
|
||||||
-- bootstrap lazy.nvim, LazyVim and your plugins
|
-- bootstrap lazy.nvim, LazyVim and your plugins
|
||||||
require("config.lazy")
|
require("config.lazy")
|
||||||
|
|
||||||
package.path = package.path .. ";./lua"
|
|
||||||
require("addons.optolith")
|
require("addons.optolith")
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
local nio = require("nio")
|
local nio = require("nio")
|
||||||
|
local git = require("functions.git")
|
||||||
|
|
||||||
local function startswith(string, start)
|
local function startswith(string, start)
|
||||||
return string:sub(1, #start) == start
|
return string:sub(1, #start) == start
|
||||||
|
|
@ -29,8 +30,7 @@ vim.api.nvim_create_user_command("Optolith", function()
|
||||||
end
|
end
|
||||||
|
|
||||||
local absOptolithDirectory = vim.fs.normalize(OPTOLITH_DIRECTORY)
|
local absOptolithDirectory = vim.fs.normalize(OPTOLITH_DIRECTORY)
|
||||||
local directoryExists = vim.fn.isdirectory(absOptolithDirectory)
|
if vim.fn.isdirectory(absOptolithDirectory) == 0 then
|
||||||
if directoryExists == 0 then
|
|
||||||
vim.notify("Can't find folder. Please start the application once.\nFolder: " .. OPTOLITH_DIRECTORY, "error", {
|
vim.notify("Can't find folder. Please start the application once.\nFolder: " .. OPTOLITH_DIRECTORY, "error", {
|
||||||
title = "Optolith",
|
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", {
|
vim.notify("Swap file already exists. To prevent data loss, please restore the swap file.", "error", {
|
||||||
title = "Optolith",
|
title = "Optolith",
|
||||||
})
|
})
|
||||||
|
return
|
||||||
end
|
end
|
||||||
if heroesFileExists then
|
if heroesFileExists then
|
||||||
local mvCommand = os.execute(string.format('mv "%s" "%s.swp"', heroesPath, heroesPath))
|
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
|
return
|
||||||
end
|
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 file = table.concat(vim.fn.readfile(currentHeroFile))
|
||||||
local fileData = vim.fn.json_decode(file)
|
local fileData = vim.fn.json_decode(file)
|
||||||
if fileData["id"] ~= nil then
|
if fileData["id"] ~= nil then
|
||||||
|
|
@ -91,5 +94,7 @@ vim.api.nvim_create_user_command("Optolith", function()
|
||||||
if swapFileExists then
|
if swapFileExists then
|
||||||
os.execute(string.format('mv "%s.swp" "%s"', heroesPath, heroesPath))
|
os.execute(string.format('mv "%s.swp" "%s"', heroesPath, heroesPath))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
git.commitFile(currentHeroFile)
|
||||||
end)
|
end)
|
||||||
end, {})
|
end, {})
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
local git = require("functions.git")
|
||||||
|
|
||||||
-- Autocmds are automatically loaded on the VeryLazy event
|
-- Autocmds are automatically loaded on the VeryLazy event
|
||||||
-- Default autocmds that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua
|
-- Default autocmds that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua
|
||||||
-- Add any additional autocmds here
|
-- Add any additional autocmds here
|
||||||
|
|
@ -5,59 +7,18 @@
|
||||||
vim.api.nvim_create_autocmd("BufWritePost", {
|
vim.api.nvim_create_autocmd("BufWritePost", {
|
||||||
pattern = { "*.md" },
|
pattern = { "*.md" },
|
||||||
callback = function(args)
|
callback = function(args)
|
||||||
local function get_file_name(file)
|
git.commitFile(args.file)
|
||||||
local file_name = file:match("[^/]*.md$")
|
|
||||||
return file_name:sub(0, #file_name - 3):gsub(" ", "_")
|
|
||||||
end
|
|
||||||
|
|
||||||
local commitMessage = get_file_name(args.file) .. "-" .. os.date("%m_%B_%Y")
|
|
||||||
|
|
||||||
os.execute('git add "' .. args.file .. '"')
|
|
||||||
local commitCommand = 'git commit -m "' .. commitMessage .. '" &> /dev/null'
|
|
||||||
local commitResult = os.execute(commitCommand)
|
|
||||||
|
|
||||||
print("Commiting... " .. commitMessage)
|
|
||||||
if commitResult == 256 then
|
|
||||||
print("Nothing to commit")
|
|
||||||
return
|
|
||||||
elseif commitResult == 0 then
|
|
||||||
print("Commit complete")
|
|
||||||
else
|
|
||||||
print("Unknown Error " .. commitResult)
|
|
||||||
print(commitCommand)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd("VimLeavePre", {
|
vim.api.nvim_create_autocmd("VimLeavePre", {
|
||||||
callback = function()
|
callback = function()
|
||||||
print("Pushing changes...")
|
git.push()
|
||||||
-- Push twice, in case the credentials expired. Ugly hack, but it works
|
|
||||||
local pushSuccess = os.execute("git push &> /dev/null")
|
|
||||||
if pushSuccess > 0 then
|
|
||||||
os.execute("git push &> /dev/null")
|
|
||||||
end
|
|
||||||
|
|
||||||
print("Done")
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd("VimEnter", {
|
vim.api.nvim_create_autocmd("VimEnter", {
|
||||||
callback = function()
|
callback = function()
|
||||||
vim.defer_fn(function()
|
git.pull()
|
||||||
local command = "git pull --rebase --quiet --no-edit &> /dev/null"
|
|
||||||
local pullSuccess = os.execute(command)
|
|
||||||
if pullSuccess == 256 then
|
|
||||||
vim.notify("Conflicts found. Please resolve and continue rebase", "error", {
|
|
||||||
title = "Updating...",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.notify("Pull success", "info", {
|
|
||||||
title = "Updating...",
|
|
||||||
})
|
|
||||||
end, 100)
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
60
.config/nvim-notes/lua/functions/git.lua
Normal file
60
.config/nvim-notes/lua/functions/git.lua
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
local nio = require("nio")
|
||||||
|
local git = {}
|
||||||
|
|
||||||
|
function git.commitFile(filepath)
|
||||||
|
local function get_file_name(file)
|
||||||
|
return vim.fn.fnamemodify(file, ":t:r")
|
||||||
|
end
|
||||||
|
|
||||||
|
local commitMessage = get_file_name(filepath) .. "-" .. os.date("%m_%B_%Y")
|
||||||
|
|
||||||
|
os.execute('git add "' .. filepath .. '"')
|
||||||
|
local commitCommand = 'git commit -m "' .. commitMessage .. '" &> /dev/null'
|
||||||
|
local commitResult = os.execute(commitCommand)
|
||||||
|
|
||||||
|
print("Commiting... " .. commitMessage)
|
||||||
|
if commitResult == 256 then
|
||||||
|
print("Nothing to commit")
|
||||||
|
return
|
||||||
|
elseif commitResult == 0 then
|
||||||
|
print("Commit complete")
|
||||||
|
else
|
||||||
|
print("Unknown Error " .. commitResult)
|
||||||
|
print(commitCommand)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function git.push()
|
||||||
|
print("Pushing changes...")
|
||||||
|
-- Push twice, in case the credentials expired. Ugly hack, but it works
|
||||||
|
local pushSuccess = os.execute("git push &> /dev/null")
|
||||||
|
if pushSuccess > 0 then
|
||||||
|
os.execute("git push &> /dev/null")
|
||||||
|
end
|
||||||
|
|
||||||
|
print("Done")
|
||||||
|
end
|
||||||
|
|
||||||
|
function git.pull()
|
||||||
|
nio.run(function()
|
||||||
|
local pullProcess = nio.process.run({
|
||||||
|
cmd = "git",
|
||||||
|
args = { "pull", "--rebase", "--quiet", "--no-edit" },
|
||||||
|
})
|
||||||
|
|
||||||
|
local pullSuccess = pullProcess.result(true)
|
||||||
|
if pullSuccess == 256 then
|
||||||
|
vim.notify("Conflicts found. Please resolve and continue rebase", "error", {
|
||||||
|
title = "Updating...",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.notify("Pull success", "info", {
|
||||||
|
title = "Updating...",
|
||||||
|
})
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
return git
|
||||||
8
.config/nvim-notes/lua/plugins/neotree.lua
Normal file
8
.config/nvim-notes/lua/plugins/neotree.lua
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
return {
|
||||||
|
"nvim-neo-tree/neo-tree.nvim",
|
||||||
|
opts = {
|
||||||
|
window = {
|
||||||
|
width = 40,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue