From b4d6eb9a232dab48ce658470a980e7a15edb500f Mon Sep 17 00:00:00 2001 From: thibthib18 <37300147+thibthib18@users.noreply.github.com> Date: Fri, 10 Sep 2021 08:50:58 +0200 Subject: [PATCH] feat: git merge branch action (#1220) --- doc/telescope.txt | 10 +++ lua/telescope/actions/init.lua | 110 ++++++++++++++++++--------------- lua/telescope/builtin/git.lua | 3 + lua/telescope/builtin/init.lua | 1 + 4 files changed, 74 insertions(+), 50 deletions(-) diff --git a/doc/telescope.txt b/doc/telescope.txt index 30f6c5d..7ad7369 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -584,6 +584,8 @@ builtin.git_branches({opts}) *builtin.git_branches()* - ``: creates a new branch, with confirmation prompt before creation - ``: deletes the currently selected branch, with confirmation prompt before deletion + - ``: 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 diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 39d31be..7e65a8f 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -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 diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua index a2be2fb..a4da679 100644 --- a/lua/telescope/builtin/git.lua +++ b/lua/telescope/builtin/git.lua @@ -297,6 +297,9 @@ git.branches = function(opts) map("i", "", actions.git_delete_branch) map("n", "", actions.git_delete_branch) + + map("i", "", actions.git_merge_branch) + map("n", "", actions.git_merge_branch) return true end, }):find() diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index a3ea4c6..e790700 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -186,6 +186,7 @@ builtin.git_bcommits = require_on_exported_call("telescope.builtin.git").bcommit --- - ``: rebases currently selected branch --- - ``: creates a new branch, with confirmation prompt before creation --- - ``: deletes the currently selected branch, with confirmation prompt before deletion +--- - ``: 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