From c5f0d05835f70f4bce15168d949563ef4c842e4d Mon Sep 17 00:00:00 2001 From: Ben Smith <37027883+smithbm2316@users.noreply.github.com> Date: Wed, 14 Apr 2021 09:31:05 +0000 Subject: [PATCH] git(action): create and checkout branch (#755) * added git action for creating and checking out a new branch, added basic docstrings for git actions * Added confirmation for creation of new branch, changed default mapping to * Switched back to `` default mapping for now --- lua/telescope/actions/init.lua | 40 ++++++++++++++++++++++++++++++++++ lua/telescope/builtin/git.lua | 5 +++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index de6c58b..bee1f48 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -297,6 +297,38 @@ actions.insert_value = function(prompt_bufnr) return entry.value end +--- Create and checkout a new git branch if it doesn't already exist +---@param prompt_bufnr number: The prompt bufnr +actions.git_create_branch = function(prompt_bufnr) + local cwd = action_state.get_current_picker(prompt_bufnr).cwd + local new_branch = action_state.get_current_line() + + if new_branch == "" then + print('Please enter the name of the new branch to create') + else + local confirmation = vim.fn.input(string.format('Create new branch "%s"? [y/n]: ', new_branch)) + if string.len(confirmation) == 0 or string.sub(string.lower(confirmation), 0, 1) ~= 'y' then + print(string.format('Didn\'t create branch "%s"', new_branch)) + return + end + + actions.close(prompt_bufnr) + + local _, ret, stderr = utils.get_os_command_output({ 'git', 'checkout', '-b', new_branch }, cwd) + if ret == 0 then + print(string.format('Switched to a new branch: %s', new_branch)) + else + print(string.format( + 'Error when creating new branch: %s Git returned "%s"', + new_branch, + table.concat(stderr, ' ') + )) + end + end +end + +--- Checkout an existing git branch +---@param prompt_bufnr number: The prompt bufnr actions.git_checkout = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() @@ -313,6 +345,8 @@ actions.git_checkout = 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() @@ -329,6 +363,8 @@ actions.git_track_branch = function(prompt_bufnr) end 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() @@ -349,6 +385,8 @@ actions.git_delete_branch = function(prompt_bufnr) end 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() @@ -369,6 +407,8 @@ actions.git_rebase_branch = function(prompt_bufnr) end end +--- Stage/unstage selected file +---@param prompt_bufnr number: The prompt bufnr actions.git_staging_toggle = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua index b2270ad..f7c0096 100644 --- a/lua/telescope/builtin/git.lua +++ b/lua/telescope/builtin/git.lua @@ -177,11 +177,12 @@ git.branches = function(opts) map('i', '', actions.git_rebase_branch) map('n', '', actions.git_rebase_branch) + map('i', '', actions.git_create_branch) + map('n', '', actions.git_create_branch) + map('i', '', actions.git_delete_branch) map('n', '', actions.git_delete_branch) - map('i', '', false) - map('n', '', false) return true end }):find()