feat: git merge branch action (#1220)
This commit is contained in:
@@ -584,6 +584,8 @@ builtin.git_branches({opts}) *builtin.git_branches()*
|
|||||||
- `<C-a>`: creates a new branch, with confirmation prompt before creation
|
- `<C-a>`: creates a new branch, with confirmation prompt before creation
|
||||||
- `<C-d>`: deletes the currently selected branch, with confirmation
|
- `<C-d>`: deletes the currently selected branch, with confirmation
|
||||||
prompt before deletion
|
prompt before deletion
|
||||||
|
- `<C-y>`: merges the currently selected branch, with confirmation prompt
|
||||||
|
before deletion
|
||||||
|
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
@@ -1488,6 +1490,14 @@ actions.git_delete_branch({prompt_bufnr}) *actions.git_delete_branch()*
|
|||||||
{prompt_bufnr} (number) The prompt bufnr
|
{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()*
|
actions.git_rebase_branch({prompt_bufnr}) *actions.git_rebase_branch()*
|
||||||
Rebase to selected git branch
|
Rebase to selected git branch
|
||||||
|
|
||||||
|
|||||||
@@ -479,65 +479,75 @@ actions.git_switch_branch = function(prompt_bufnr)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Tell git to track the currently selected remote branch in Telescope
|
local function make_git_branch_action(opts)
|
||||||
---@param prompt_bufnr number: The prompt bufnr
|
return function(prompt_bufnr)
|
||||||
actions.git_track_branch = function(prompt_bufnr)
|
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
||||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
local selection = action_state.get_selected_entry()
|
||||||
local selection = action_state.get_selected_entry()
|
|
||||||
actions.close(prompt_bufnr)
|
local should_confirm = opts.should_confirm
|
||||||
local _, ret, stderr = utils.get_os_command_output({ "git", "checkout", "--track", selection.value }, cwd)
|
if should_confirm then
|
||||||
if ret == 0 then
|
local confirmation = vim.fn.input(string.format(opts.confirmation_question, selection.value))
|
||||||
print("Tracking branch: " .. selection.value)
|
if confirmation ~= "" and string.lower(confirmation) ~= "y" then
|
||||||
else
|
return
|
||||||
print(
|
end
|
||||||
string.format('Error when tracking branch: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))
|
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
|
||||||
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
|
--- Delete the currently selected branch
|
||||||
---@param prompt_bufnr number: The prompt bufnr
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
actions.git_delete_branch = function(prompt_bufnr)
|
actions.git_delete_branch = make_git_branch_action {
|
||||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
should_confirm = true,
|
||||||
local selection = action_state.get_selected_entry()
|
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] ")
|
--- Merge the currently selected branch
|
||||||
if confirmation ~= "" and string.lower(confirmation) ~= "y" then
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
return
|
actions.git_merge_branch = make_git_branch_action {
|
||||||
end
|
should_confirm = true,
|
||||||
|
confirmation_question = "Do you really wanna merge branch %s? [Y/n] ",
|
||||||
actions.close(prompt_bufnr)
|
success_message = "Merged branch: %s",
|
||||||
local _, ret, stderr = utils.get_os_command_output({ "git", "branch", "-D", selection.value }, cwd)
|
error_message = 'Error when merging branch: %s. Git returned: "%s"',
|
||||||
if ret == 0 then
|
command = function(branch_name)
|
||||||
print("Deleted branch: " .. selection.value)
|
return { "git", "merge", branch_name }
|
||||||
else
|
end,
|
||||||
print(
|
}
|
||||||
string.format('Error when deleting branch: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Rebase to selected git branch
|
--- Rebase to selected git branch
|
||||||
---@param prompt_bufnr number: The prompt bufnr
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
actions.git_rebase_branch = function(prompt_bufnr)
|
actions.git_rebase_branch = make_git_branch_action {
|
||||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
should_confirm = true,
|
||||||
local selection = action_state.get_selected_entry()
|
confirmation_question = "Do you really wanna rebase branch %s? [Y/n] ",
|
||||||
|
success_message = "Rebased branch: %s",
|
||||||
local confirmation = vim.fn.input("Do you really wanna rebase branch " .. selection.value .. "? [Y/n] ")
|
error_message = 'Error when rebasing branch: %s. Git returned: "%s"',
|
||||||
if confirmation ~= "" and string.lower(confirmation) ~= "y" then
|
command = function(branch_name)
|
||||||
return
|
return { "git", "rebase", branch_name }
|
||||||
end
|
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
|
|
||||||
|
|
||||||
local git_reset_branch = function(prompt_bufnr, mode)
|
local git_reset_branch = function(prompt_bufnr, mode)
|
||||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
||||||
|
|||||||
@@ -297,6 +297,9 @@ git.branches = function(opts)
|
|||||||
|
|
||||||
map("i", "<c-d>", actions.git_delete_branch)
|
map("i", "<c-d>", actions.git_delete_branch)
|
||||||
map("n", "<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
|
return true
|
||||||
end,
|
end,
|
||||||
}):find()
|
}):find()
|
||||||
|
|||||||
@@ -186,6 +186,7 @@ builtin.git_bcommits = require_on_exported_call("telescope.builtin.git").bcommit
|
|||||||
--- - `<C-r>`: rebases currently selected branch
|
--- - `<C-r>`: rebases currently selected branch
|
||||||
--- - `<C-a>`: creates a new branch, with confirmation prompt before creation
|
--- - `<C-a>`: creates a new branch, with confirmation prompt before creation
|
||||||
--- - `<C-d>`: deletes the currently selected branch, with confirmation prompt before deletion
|
--- - `<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
|
---@param opts table: options to pass to the picker
|
||||||
builtin.git_branches = require_on_exported_call("telescope.builtin.git").branches
|
builtin.git_branches = require_on_exported_call("telescope.builtin.git").branches
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user