Obsidian: Replace gitscripts with neovim jobs

This commit is contained in:
ViViDboarder 2024-11-27 13:43:17 -08:00
parent bf630d6bbf
commit f5cacea14b

View File

@ -643,25 +643,6 @@ return {
-- and commits to my vault git repo. On iOS devices, I use Working Copy to sync the -- and commits to my vault git repo. On iOS devices, I use Working Copy to sync the
-- repo and use Shortcuts to automate pulling on open and auto committing and pushing -- repo and use Shortcuts to automate pulling on open and auto committing and pushing
-- after closing Obsidian. -- after closing Obsidian.
{
-- Fork of https://github.com/declancm/git-scripts.nvim is used here
-- because it includes a few small fixes.
"https://github.com/vividboarder/git-scripts.nvim",
branch = "dev",
config = function()
local gs = require("git-scripts")
gs.setup({
-- Disable keymaps becasue I only use this for auto pull and auto commit
default_keymaps = false,
commit_on_save = true,
})
gs.async_pull()
end,
event = {
"BufRead " .. vim.fn.expand("~") .. "/Documents/Obsidian/**.md",
"BufNewFile " .. vim.fn.expand("~") .. "/Documents/Obsidian/**.md",
},
},
{ {
"https://github.com/epwalsh/obsidian.nvim", "https://github.com/epwalsh/obsidian.nvim",
dependencies = { dependencies = {
@ -680,9 +661,124 @@ return {
external_link_icon = { char = "🔗", hl_group = "ObsidianExtLinkIcon" }, external_link_icon = { char = "🔗", hl_group = "ObsidianExtLinkIcon" },
}, },
}, },
config = function(opts)
-- Setup obsidian
require("obsidian").setup(opts)
-- Set up auto pull and commit
local group_id = vim.api.nvim_create_augroup("obsidian-git", { clear = true })
-- Create auto pull on open
local autopull = function()
local Job = require("plenary.job")
vim.notify("Pulling Obsidian notes", vim.log.levels.INFO, { title = "Obsidian" })
Job:new({
command = "git",
args = { "pull" },
on_exit = function(j, return_val)
if return_val == 0 then
vim.notify("Pulled Obsidian notes", vim.log.levels.INFO, { title = "Obsidian" })
else
vim.notify(
"Failed to pull Obsidian notes. " .. vim.inspect(j:result()),
vim.log.levels.ERROR,
{ title = "Obsidian" }
)
end
end,
}):start()
end
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
pattern = vim.fn.expand("~") .. "/Documents/Obsidian/**",
callback = autopull,
group = group_id,
})
local Job = require("plenary.job")
-- Create autocommit on save
local auto_add = function(next_func)
return function(ev)
Job:new({
command = "git",
args = { "add", ev.file },
on_exit = function(add_j, add_return_val)
if add_return_val ~= 0 then
vim.notify(
"Failed to add file to git. " .. vim.inspect(add_j:result()),
vim.log.levels.ERROR,
{ title = "Obsidian" }
)
return
end
if next_func then
next_func()
end
end,
}):start()
end
end
local auto_commit = function(next_func)
return function()
local date_string = os.date("%Y-%m-%d %H:%M:%S")
Job
:new({
command = "git",
args = { "commit", "-m", "Auto commit: " .. date_string },
on_exit = function(commit_j, commit_return_val)
if commit_return_val ~= 0 then
vim.notify(
"Failed to commit file to git. " .. vim.inspect(commit_j:result()),
vim.log.levels.ERROR,
{ title = "Obsidian" }
)
return
end
if next_func then
next_func()
end
end,
})
:start()
end
end
local auto_push = function(next_func)
return function()
Job
:new({
command = "git",
args = { "push" },
on_exit = function(push_j, push_return_val)
if push_return_val ~= 0 then
vim.notify(
"Failed to push Obsidian notes. " .. vim.inspect(push_j:result()),
vim.log.levels.ERROR,
{ title = "Obsidian" }
)
end
if next_func then
next_func()
end
end,
})
:start()
end
end
vim.api.nvim_create_autocmd({ "BufWritePost" }, {
pattern = vim.fn.expand("~") .. "/Documents/Obsidian/**",
callback = auto_add(auto_commit(auto_push())),
group = group_id,
})
end,
event = { event = {
"BufRead " .. vim.fn.expand("~") .. "/Documents/Obsidian/**.md", "BufRead " .. vim.fn.expand("~") .. "/Documents/Obsidian/**",
"BufNewFile " .. vim.fn.expand("~") .. "/Documents/Obsidian/**.md", "BufNewFile " .. vim.fn.expand("~") .. "/Documents/Obsidian/**",
}, },
}, },