Skip to content

Commit 8676c40

Browse files
IanLiuTWIan Liufolke
authored
feat(picker.git): add create and delete branch to git_branches (#909)
## Description <!-- Describe the big picture of your changes to communicate to the maintainers why we should accept this pull request. --> This PR adds two actions: `git_create_branch` and `git_delete_branch`. They are aimed to be included in the `git_branches` picker to easily create/delete git branches with custom keymaps. ## Related Issue(s) <!-- If this PR fixes any issues, please link to the issue here. - Fixes #<issue_number> --> ## Screenshots <!-- Add screenshots of the changes if applicable. --> --------- Co-authored-by: Ian Liu <[email protected]> Co-authored-by: Folke Lemaitre <[email protected]>
1 parent 6069a1e commit 8676c40

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

doc/snacks-picker.txt

+8
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,14 @@ GIT_BRANCHES *snacks-picker-sources-git_branches*
12041204
format = "git_branch",
12051205
preview = "git_log",
12061206
confirm = "git_checkout",
1207+
win = {
1208+
input = {
1209+
keys = {
1210+
["<c-a>"] = { "git_create_branch", mode = { "n", "i" } },
1211+
["<c-x>"] = { "git_delete_branch", mode = { "n", "i" } },
1212+
},
1213+
},
1214+
},
12071215
on_show = function(picker)
12081216
for i, item in ipairs(picker:items()) do
12091217
if item.current then

lua/snacks/picker/actions.lua

+47
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,53 @@ function M.git_checkout(picker, item)
314314
end
315315
end
316316

317+
function M.git_branch_add(picker)
318+
Snacks.input.input({
319+
prompt = "New Branch Name",
320+
default = picker.input:get(),
321+
}, function(name)
322+
if (name or ""):match("^%s*$") then
323+
return
324+
end
325+
Snacks.picker.util.cmd({ "git", "branch", "--list", name }, function(data)
326+
if data[1] ~= "" then
327+
return Snacks.notify.error("Branch '" .. name .. "' already exists.", { title = "Snacks Picker" })
328+
end
329+
Snacks.picker.util.cmd({ "git", "checkout", "-b", name }, function()
330+
Snacks.notify("Created Branch `" .. name .. "`", { title = "Snacks Picker" })
331+
vim.cmd.checktime()
332+
picker.list:set_target()
333+
picker.input:set("", "")
334+
picker:find()
335+
end, { cwd = picker:cwd() })
336+
end, { cwd = picker:cwd() })
337+
end)
338+
end
339+
340+
function M.git_branch_del(picker, item)
341+
if not (item and item.branch) then
342+
Snacks.notify.warn("No branch or commit found", { title = "Snacks Picker" })
343+
end
344+
345+
local branch = item.branch
346+
Snacks.picker.util.cmd({ "git", "rev-parse", "--abbrev-ref", "HEAD" }, function(data)
347+
-- Check if we are on the same branch
348+
if data[1]:match(branch) ~= nil then
349+
Snacks.notify.error("Cannot delete the current branch.", { title = "Snacks Picker" })
350+
return
351+
end
352+
353+
-- Proceed with deletion
354+
Snacks.picker.util.cmd({ "git", "branch", "-d", branch }, function()
355+
Snacks.notify("Deleted Branch `" .. branch .. "`", { title = "Snacks Picker" })
356+
vim.cmd.checktime()
357+
picker.list:set_selected()
358+
picker.list:set_target()
359+
picker:find()
360+
end, { cwd = picker:cwd() })
361+
end, { cwd = picker:cwd() })
362+
end
363+
317364
---@param items snacks.picker.Item[]
318365
---@param opts? {win?:number}
319366
local function setqflist(items, opts)

lua/snacks/picker/config/sources.lua

+8
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ M.git_branches = {
191191
format = "git_branch",
192192
preview = "git_log",
193193
confirm = "git_checkout",
194+
win = {
195+
input = {
196+
keys = {
197+
["<c-a>"] = { "git_branch_add", mode = { "n", "i" } },
198+
["<c-x>"] = { "git_branch_del", mode = { "n", "i" } },
199+
},
200+
},
201+
},
194202
on_show = function(picker)
195203
for i, item in ipairs(picker:items()) do
196204
if item.current then

0 commit comments

Comments
 (0)