yadm-config/.config/nvim-notes/lua/config/autocmds.lua

55 lines
1.6 KiB
Lua

-- 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
-- Add any additional autocmds here
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = { "*.md" },
callback = function(args)
local function get_file_name(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,
})
vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function()
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,
})
vim.api.nvim_create_autocmd("VimEnter", {
callback = function()
print("Updating...")
local command = "git pull --rebase --quiet --no-edit &> /dev/null"
local pullSuccess = os.execute(command)
if pullSuccess > 0 then
os.execute(command)
end
end,
})