Skip to content

Commit

Permalink
Merge pull request ThePrimeagen#12 from ThePrimeagen/enum
Browse files Browse the repository at this point in the history
Enum + git push
  • Loading branch information
ThePrimeagen committed Apr 14, 2021
2 parents 6de685a + d8abfa4 commit 42b80e4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
22 changes: 22 additions & 0 deletions lua/git-worktree/enum.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local Enum = function(tbl)
return setmetatable(tbl, {
__index = function(_, key)
error(string.format("%s does not exist for this enum.", key))
end,

__newindex = function(t, key, value)
error("Enums are immutable. You are not able to set new values")
end,
})
end

return {
Operations = Enum({
Create = "create",
Switch = "switch",
Delete = "delete",
})
}



47 changes: 35 additions & 12 deletions lua/git-worktree/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Job = require("plenary.job")
local Path = require("plenary.path")
local Enum = require("git-worktree.enum")

local Status = require("git-worktree.status")

Expand All @@ -10,7 +11,7 @@ local on_change_callbacks = {}

local function on_tree_change_handler(op, path, _) -- _ = upstream
if M._config.update_on_change then
if op == "switch" then
if op == Enum.Operations.Switch then
local changed = M.update_current_buffer()
if not changed then
vim.cmd(string.format(":Ex %s", M.get_worktree_path(path)))
Expand All @@ -20,14 +21,12 @@ local function on_tree_change_handler(op, path, _) -- _ = upstream
end

local function emit_on_change(op, path, upstream)

-- TODO: We don't have a way to async update what is running
status:next_status(string.format("Running post %s callbacks", op))
on_tree_change_handler(op, path, upstream)
for idx = 1, #on_change_callbacks do
on_change_callbacks[idx](op, path, upstream)
end

end

local function change_dirs(path)
Expand Down Expand Up @@ -86,15 +85,21 @@ local function has_worktree(path, cb)
job:start()
end

local function failure(from, cmd, path)
local function failure(from, cmd, path, soft_error)
return function(e)
status:error(string.format(
local error_message = string.format(
"%s Failed: PATH %s CMD %s RES %s, ERR %s",
from,
path,
vim.inspect(cmd),
vim.inspect(e:result()),
vim.inspect(e:stderr_result())))
vim.inspect(e:stderr_result()))

if soft_error then
status:status(error_message)
else
status:error(error_message)
end
end
end

Expand Down Expand Up @@ -136,6 +141,17 @@ local function create_worktree(path, upstream, found_branch)
end
})

-- TODO: How to configure origin??? Should upstream ever be the push
-- destination?
local set_push = Job:new({
'git', 'push', "--set-upstream", "origin", path,
cwd = worktree_path,
on_start = function()
status:next_status("git set_branch")
end
})


local rebase = Job:new({
'git', 'rebase',
cwd = worktree_path,
Expand All @@ -146,11 +162,17 @@ local function create_worktree(path, upstream, found_branch)

create:and_then_on_success(fetch)
fetch:and_then_on_success(set_branch)
set_branch:and_then_on_success(rebase)

-- These are "optional" operations.
-- We have to figure out how we want to handle these...
set_branch:and_then(set_push)
set_push:and_then(rebase)

create:after_failure(failure("create_worktree", create.args, git_worktree_root))
fetch:after_failure(failure("create_worktree", fetch.args, worktree_path))
set_branch:after_failure(failure("create_worktree", set_branch.args, worktree_path))

set_branch:after_failure(failure("create_worktree", set_branch.args, worktree_path, true))
set_push:after_failure(failure("create_worktree", set_branch.args, worktree_path, true))

rebase:after(function()

Expand All @@ -159,7 +181,7 @@ local function create_worktree(path, upstream, found_branch)
end

vim.schedule(function()
emit_on_change("create", path, upstream)
emit_on_change(Enum.Operations.Create, path, upstream)
M.switch_worktree(path)
end)
end)
Expand All @@ -168,7 +190,7 @@ local function create_worktree(path, upstream, found_branch)
end

M.create_worktree = function(path, upstream)
status:reset(7)
status:reset(8)

if upstream == nil then
error("Please provide an upstream...")
Expand Down Expand Up @@ -196,7 +218,7 @@ M.switch_worktree = function(path)

vim.schedule(function()
change_dirs(path)
emit_on_change("switch", path)
emit_on_change(Enum.Operations.Switch, path)
end)

end)
Expand All @@ -219,7 +241,7 @@ M.delete_worktree = function(path, force)

local delete = Job:new(cmd)
delete:after_success(vim.schedule_wrap(function()
emit_on_change("delete", path)
emit_on_change(Enum.Operations.Delete, path)
end))

delete:after_failure(failure(cmd, vim.loop.cwd()))
Expand Down Expand Up @@ -299,5 +321,6 @@ M.set_status = function(msg)
end

M.setup()
M.Operations = Enum.Operations

return M

0 comments on commit 42b80e4

Please sign in to comment.