summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Smith <37027883+smithbm2316@users.noreply.github.com>2021-04-14 09:31:05 +0000
committerGitHub <noreply@github.com>2021-04-14 12:31:05 +0300
commitc5f0d05835f70f4bce15168d949563ef4c842e4d (patch)
treeb729147e6922da06de5d3d7a3298efd0e1992484
parentb7d0488db91240bed3afe4b82e5c974836ee060f (diff)
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 <c-u> * Switched back to `<c-a>` default mapping for now
-rw-r--r--lua/telescope/actions/init.lua40
-rw-r--r--lua/telescope/builtin/git.lua5
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', '<c-r>', actions.git_rebase_branch)
map('n', '<c-r>', actions.git_rebase_branch)
+ map('i', '<c-a>', actions.git_create_branch)
+ map('n', '<c-a>', actions.git_create_branch)
+
map('i', '<c-d>', actions.git_delete_branch)
map('n', '<c-d>', actions.git_delete_branch)
- map('i', '<c-u>', false)
- map('n', '<c-u>', false)
return true
end
}):find()