Skip to content

Commit

Permalink
chore(updater): cleanup some code
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Jun 7, 2022
1 parent 3003b03 commit efa8b48
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions lua/core/utils/updater.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ local function pretty_changelog(commits)
return changelog
end

local function attempt_update(target)
if options.channel == "stable" or options.commit then
return git.checkout(target, false)
else
return git.pull(false)
end
end

function astronvim.updater.update()
for remote, entry in pairs(options.remotes and options.remotes or {}) do
local url = git.parse_remote_url(entry)
Expand Down Expand Up @@ -74,6 +82,7 @@ function astronvim.updater.update()
options.branch = is_stable and "main" or options.branch
if not git.fetch(options.remote) then
vim.api.nvim_err_writeln("Error fetching remote: " .. options.remote)
return
end
local local_branch = (options.remote == "origin" and "" or (options.remote .. "_")) .. options.branch
if git.current_branch() ~= local_branch then
Expand All @@ -91,6 +100,7 @@ function astronvim.updater.update()
end
local source = git.local_head() -- calculate current commit
local target -- calculate target commit
astronvim.echo { { "Checking for new updates...\n\n" } }
if is_stable then -- if stable get tag commit
options.version = git.latest_version(git.get_versions(options.version or "latest"))
target = git.tag_commit(options.version)
Expand All @@ -99,66 +109,61 @@ function astronvim.updater.update()
else -- get most recent commit
target = git.remote_head(options.remote, options.branch)
end
if source and target then -- continue if current and target commits were found
if source == target then
astronvim.echo { { "No updates available", "String" } }
elseif -- prompt user if they want to accept update
not options.skip_prompts
if not source or not target then -- continue if current and target commits were found
vim.api.nvim_err_writeln "Error checking for updates"
return
elseif source == target then
astronvim.echo { { "No updates available", "String" } }
return
elseif -- prompt user if they want to accept update
not options.skip_prompts
and not astronvim.confirm_prompt {
{ "Update available to ", "Title" },
{ is_stable and options.version or target, "String" },
{ "\nContinue?" },
}
then
echo_cancelled()
return
else -- perform update
local changelog = git.get_commit_range(source, target)
local breaking = git.breaking_changes(changelog)
local breaking_prompt = { { "Update contains the following breaking changes:\n", "WarningMsg" } }
vim.list_extend(breaking_prompt, pretty_changelog(breaking))
vim.list_extend(breaking_prompt, { { "\nWould you like to continue?" } })
if #breaking > 0 and not options.skip_prompts and not astronvim.confirm_prompt(breaking_prompt) then
echo_cancelled()
return
end
local updated = attempt_update(target)
if
not updated
and not options.skip_prompts
and not astronvim.confirm_prompt {
{ "Update available to ", "Title" },
{ is_stable and options.version or target, "String" },
{ "\nContinue?" },
{ "Unable to pull due to local modifications to base files.\n", "ErrorMsg" },
{ "Reset local files and continue?" },
}
then
echo_cancelled()
return
else -- perform update
local changelog = git.get_commit_range(source, target)
local breaking = git.breaking_changes(changelog)
local breaking_prompt = { { "Update contains the following breaking changes:\n", "WarningMsg" } }
vim.list_extend(breaking_prompt, pretty_changelog(breaking))
vim.list_extend(breaking_prompt, { { "\nWould you like to continue?" } })
if #breaking > 0 and not options.skip_prompts and not astronvim.confirm_prompt(breaking_prompt) then
echo_cancelled()
return
end
local function attempt_update() -- helper function to attempt an update
if is_stable or options.commit then
return git.checkout(target, false)
else
return git.pull(false)
end
end
local updated = attempt_update()
if
not updated
and not options.skip_prompts
and not astronvim.confirm_prompt {
{ "Unable to pull due to local modifications to base files.\n", "ErrorMsg" },
{ "Reset local files and continue?" },
}
then
echo_cancelled()
return
elseif not updated then
git.hard_reset(source)
updated = attempt_update()
end
if not updated then
vim.api.nvim_err_writeln "Error ocurred performing update"
return
end
local summary = {
{ "AstroNvim updated successfully to ", "Title" },
{ git.current_version(), "String" },
{ "!\n", "Title" },
{ "Please restart and run :PackerSync.\n\n", "WarningMsg" },
}
if #changelog > 0 and options.show_changelog then
vim.list_extend(summary, { { "Changelog:\n" } })
vim.list_extend(summary, pretty_changelog(changelog))
end
astronvim.echo(summary)
elseif not updated then
git.hard_reset(source)
updated = attempt_update(target)
end
if not updated then
vim.api.nvim_err_writeln "Error ocurred performing update"
return
end
local summary = {
{ "AstroNvim updated successfully to ", "Title" },
{ git.current_version(), "String" },
{ "!\n", "Title" },
{ "Please restart and run :PackerSync.\n\n", "WarningMsg" },
}
if options.show_changelog and #changelog > 0 then
vim.list_extend(summary, { { "Changelog:\n" } })
vim.list_extend(summary, pretty_changelog(changelog))
end
astronvim.echo(summary)
end
end

0 comments on commit efa8b48

Please sign in to comment.