feat: git merge branch action (#1220)

This commit is contained in:
thibthib18
2021-09-10 08:50:58 +02:00
committed by GitHub
parent d2b768983c
commit b4d6eb9a23
4 changed files with 74 additions and 50 deletions

View File

@@ -584,6 +584,8 @@ builtin.git_branches({opts}) *builtin.git_branches()*
- `<C-a>`: creates a new branch, with confirmation prompt before creation
- `<C-d>`: deletes the currently selected branch, with confirmation
prompt before deletion
- `<C-y>`: merges the currently selected branch, with confirmation prompt
before deletion
Parameters: ~
@@ -1488,6 +1490,14 @@ actions.git_delete_branch({prompt_bufnr}) *actions.git_delete_branch()*
{prompt_bufnr} (number) The prompt bufnr
actions.git_merge_branch({prompt_bufnr}) *actions.git_merge_branch()*
Merge the currently selected branch
Parameters: ~
{prompt_bufnr} (number) The prompt bufnr
actions.git_rebase_branch({prompt_bufnr}) *actions.git_rebase_branch()*
Rebase to selected git branch

View File

@@ -479,65 +479,75 @@ actions.git_switch_branch = function(prompt_bufnr)
end
end
--- Tell git to track the currently selected remote branch in Telescope
---@param prompt_bufnr number: The prompt bufnr
actions.git_track_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output({ "git", "checkout", "--track", selection.value }, cwd)
if ret == 0 then
print("Tracking branch: " .. selection.value)
else
print(
string.format('Error when tracking branch: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))
)
local function make_git_branch_action(opts)
return function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
local should_confirm = opts.should_confirm
if should_confirm then
local confirmation = vim.fn.input(string.format(opts.confirmation_question, selection.value))
if confirmation ~= "" and string.lower(confirmation) ~= "y" then
return
end
end
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output(opts.command(selection.value), cwd)
if ret == 0 then
print(string.format(opts.success_message, selection.value))
else
print(string.format(opts.error_message, selection.value, table.concat(stderr, " ")))
end
end
end
--- Tell git to track the currently selected remote branch in Telescope
---@param prompt_bufnr number: The prompt bufnr
actions.git_track_branch = make_git_branch_action {
should_confirm = false,
success_message = "Tracking branch: %s",
error_message = 'Error when tracking branch: %s. Git returned: "%s"',
command = function(branch_name)
return { "git", "checkout", "--track", branch_name }
end,
}
--- Delete the currently selected branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_delete_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
actions.git_delete_branch = make_git_branch_action {
should_confirm = true,
confirmation_question = "Do you really wanna delete branch %s? [Y/n] ",
success_message = "Deleted branch: %s",
error_message = 'Error when deleting branch: %s. Git returned: "%s"',
command = function(branch_name)
return { "git", "branch", "-D", branch_name }
end,
}
local confirmation = vim.fn.input("Do you really wanna delete branch " .. selection.value .. "? [Y/n] ")
if confirmation ~= "" and string.lower(confirmation) ~= "y" then
return
end
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output({ "git", "branch", "-D", selection.value }, cwd)
if ret == 0 then
print("Deleted branch: " .. selection.value)
else
print(
string.format('Error when deleting branch: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))
)
end
end
--- Merge the currently selected branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_merge_branch = make_git_branch_action {
should_confirm = true,
confirmation_question = "Do you really wanna merge branch %s? [Y/n] ",
success_message = "Merged branch: %s",
error_message = 'Error when merging branch: %s. Git returned: "%s"',
command = function(branch_name)
return { "git", "merge", branch_name }
end,
}
--- Rebase to selected git branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_rebase_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
local confirmation = vim.fn.input("Do you really wanna rebase branch " .. selection.value .. "? [Y/n] ")
if confirmation ~= "" and string.lower(confirmation) ~= "y" then
return
end
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output({ "git", "rebase", selection.value }, cwd)
if ret == 0 then
print("Rebased branch: " .. selection.value)
else
print(
string.format('Error when rebasing branch: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))
)
end
end
actions.git_rebase_branch = make_git_branch_action {
should_confirm = true,
confirmation_question = "Do you really wanna rebase branch %s? [Y/n] ",
success_message = "Rebased branch: %s",
error_message = 'Error when rebasing branch: %s. Git returned: "%s"',
command = function(branch_name)
return { "git", "rebase", branch_name }
end,
}
local git_reset_branch = function(prompt_bufnr, mode)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd

View File

@@ -297,6 +297,9 @@ git.branches = function(opts)
map("i", "<c-d>", actions.git_delete_branch)
map("n", "<c-d>", actions.git_delete_branch)
map("i", "<c-y>", actions.git_merge_branch)
map("n", "<c-y>", actions.git_merge_branch)
return true
end,
}):find()

View File

@@ -186,6 +186,7 @@ builtin.git_bcommits = require_on_exported_call("telescope.builtin.git").bcommit
--- - `<C-r>`: rebases currently selected branch
--- - `<C-a>`: creates a new branch, with confirmation prompt before creation
--- - `<C-d>`: deletes the currently selected branch, with confirmation prompt before deletion
--- - `<C-y>`: merges the currently selected branch, with confirmation prompt before deletion
---@param opts table: options to pass to the picker
builtin.git_branches = require_on_exported_call("telescope.builtin.git").branches