85 lines
2.7 KiB
Lua
85 lines
2.7 KiB
Lua
local git = require('repl_plugin.git')
|
|
local M = {}
|
|
|
|
local api = vim.api
|
|
local fn = vim.fn
|
|
|
|
local config = {}
|
|
local last_branch = nil
|
|
local timer_id = nil
|
|
|
|
function M.setup(cfg)
|
|
config = cfg or {}
|
|
end
|
|
|
|
function M.sync_once(cb)
|
|
cb = cb or function() end
|
|
git.get_branch(config.main_repo_path, function(err, branch)
|
|
if err or not branch or branch == '' then
|
|
vim.notify('Could not determine main repo branch', vim.log.levels.WARN)
|
|
return cb('no_branch')
|
|
end
|
|
if branch == last_branch then
|
|
return cb(nil, {changed=false})
|
|
end
|
|
|
|
-- attempt to commit repl repo if dirty
|
|
git.commit_if_dirty(config.repl_repo_path, {auto_commit = config.auto_commit}, function(cerr, cres)
|
|
if cerr then
|
|
vim.notify('Failed to commit REPL repo: ' .. tostring(cerr), vim.log.levels.ERROR)
|
|
return cb('commit_failed')
|
|
end
|
|
-- proceed to ensure branch exists and checkout
|
|
git.branch_exists(config.repl_repo_path, branch, function(_, exists)
|
|
if exists then
|
|
git.checkout_branch(config.repl_repo_path, branch, function(co_err, ok)
|
|
if co_err then
|
|
vim.notify('Failed to checkout repl branch: ' .. tostring(co_err), vim.log.levels.ERROR)
|
|
return cb('checkout_failed')
|
|
end
|
|
last_branch = branch
|
|
vim.notify('Repl switched to branch: ' .. branch, vim.log.levels.INFO)
|
|
return cb(nil, {changed=true, branch=branch})
|
|
end)
|
|
else
|
|
-- prefer template
|
|
git.branch_exists(config.repl_repo_path, 'template', function(_, tmpl)
|
|
local from = tmpl and 'template' or 'HEAD'
|
|
git.create_branch(config.repl_repo_path, branch, from, function(cr_err, ok)
|
|
if cr_err then
|
|
vim.notify('Failed to create repl branch: ' .. tostring(cr_err), vim.log.levels.ERROR)
|
|
return cb('create_failed')
|
|
end
|
|
last_branch = branch
|
|
vim.notify('Repl branch created: ' .. branch, vim.log.levels.INFO)
|
|
return cb(nil, {changed=true, branch=branch})
|
|
end)
|
|
end)
|
|
end
|
|
end)
|
|
end)
|
|
end)
|
|
end
|
|
|
|
function M.start(interval)
|
|
if timer_id then
|
|
vim.notify('REPL sync already running', vim.log.levels.INFO)
|
|
return
|
|
end
|
|
interval = interval or config.poll_interval or 5000
|
|
timer_id = fn.timer_start(interval, function()
|
|
vim.schedule(function() M.sync_once() end)
|
|
end, {['repeat'] = -1})
|
|
vim.notify('Started REPL sync (interval ' .. tostring(interval) .. ' ms)', vim.log.levels.INFO)
|
|
end
|
|
|
|
function M.stop()
|
|
if timer_id then
|
|
pcall(fn.timer_stop, timer_id)
|
|
timer_id = nil
|
|
vim.notify('Stopped REPL sync', vim.log.levels.INFO)
|
|
end
|
|
end
|
|
|
|
return M
|