Skip to content

Commit c5f0d05

Browse files
authored
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
1 parent b7d0488 commit c5f0d05

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

lua/telescope/actions/init.lua

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,38 @@ actions.insert_value = function(prompt_bufnr)
297297
return entry.value
298298
end
299299

300+
--- Create and checkout a new git branch if it doesn't already exist
301+
---@param prompt_bufnr number: The prompt bufnr
302+
actions.git_create_branch = function(prompt_bufnr)
303+
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
304+
local new_branch = action_state.get_current_line()
305+
306+
if new_branch == "" then
307+
print('Please enter the name of the new branch to create')
308+
else
309+
local confirmation = vim.fn.input(string.format('Create new branch "%s"? [y/n]: ', new_branch))
310+
if string.len(confirmation) == 0 or string.sub(string.lower(confirmation), 0, 1) ~= 'y' then
311+
print(string.format('Didn\'t create branch "%s"', new_branch))
312+
return
313+
end
314+
315+
actions.close(prompt_bufnr)
316+
317+
local _, ret, stderr = utils.get_os_command_output({ 'git', 'checkout', '-b', new_branch }, cwd)
318+
if ret == 0 then
319+
print(string.format('Switched to a new branch: %s', new_branch))
320+
else
321+
print(string.format(
322+
'Error when creating new branch: %s Git returned "%s"',
323+
new_branch,
324+
table.concat(stderr, ' ')
325+
))
326+
end
327+
end
328+
end
329+
330+
--- Checkout an existing git branch
331+
---@param prompt_bufnr number: The prompt bufnr
300332
actions.git_checkout = function(prompt_bufnr)
301333
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
302334
local selection = action_state.get_selected_entry()
@@ -313,6 +345,8 @@ actions.git_checkout = function(prompt_bufnr)
313345
end
314346
end
315347

348+
--- Tell git to track the currently selected remote branch in Telescope
349+
---@param prompt_bufnr number: The prompt bufnr
316350
actions.git_track_branch = function(prompt_bufnr)
317351
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
318352
local selection = action_state.get_selected_entry()
@@ -329,6 +363,8 @@ actions.git_track_branch = function(prompt_bufnr)
329363
end
330364
end
331365

366+
--- Delete the currently selected branch
367+
---@param prompt_bufnr number: The prompt bufnr
332368
actions.git_delete_branch = function(prompt_bufnr)
333369
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
334370
local selection = action_state.get_selected_entry()
@@ -349,6 +385,8 @@ actions.git_delete_branch = function(prompt_bufnr)
349385
end
350386
end
351387

388+
--- Rebase to selected git branch
389+
---@param prompt_bufnr number: The prompt bufnr
352390
actions.git_rebase_branch = function(prompt_bufnr)
353391
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
354392
local selection = action_state.get_selected_entry()
@@ -369,6 +407,8 @@ actions.git_rebase_branch = function(prompt_bufnr)
369407
end
370408
end
371409

410+
--- Stage/unstage selected file
411+
---@param prompt_bufnr number: The prompt bufnr
372412
actions.git_staging_toggle = function(prompt_bufnr)
373413
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
374414
local selection = action_state.get_selected_entry()

lua/telescope/builtin/git.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,12 @@ git.branches = function(opts)
177177
map('i', '<c-r>', actions.git_rebase_branch)
178178
map('n', '<c-r>', actions.git_rebase_branch)
179179

180+
map('i', '<c-a>', actions.git_create_branch)
181+
map('n', '<c-a>', actions.git_create_branch)
182+
180183
map('i', '<c-d>', actions.git_delete_branch)
181184
map('n', '<c-d>', actions.git_delete_branch)
182185

183-
map('i', '<c-u>', false)
184-
map('n', '<c-u>', false)
185186
return true
186187
end
187188
}):find()

0 commit comments

Comments
 (0)