mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-04 18:58:12 +00:00
feat(picker.git): add create and delete branch to git_branches (#909)
Some checks failed
CI / ci (push) Failing after 0s
Some checks failed
CI / ci (push) Failing after 0s
## 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 <ian.liu@tupl.com> Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
parent
6069a1e4df
commit
8676c409e1
3 changed files with 63 additions and 0 deletions
|
@ -314,6 +314,53 @@ function M.git_checkout(picker, item)
|
|||
end
|
||||
end
|
||||
|
||||
function M.git_branch_add(picker)
|
||||
Snacks.input.input({
|
||||
prompt = "New Branch Name",
|
||||
default = picker.input:get(),
|
||||
}, function(name)
|
||||
if (name or ""):match("^%s*$") then
|
||||
return
|
||||
end
|
||||
Snacks.picker.util.cmd({ "git", "branch", "--list", name }, function(data)
|
||||
if data[1] ~= "" then
|
||||
return Snacks.notify.error("Branch '" .. name .. "' already exists.", { title = "Snacks Picker" })
|
||||
end
|
||||
Snacks.picker.util.cmd({ "git", "checkout", "-b", name }, function()
|
||||
Snacks.notify("Created Branch `" .. name .. "`", { title = "Snacks Picker" })
|
||||
vim.cmd.checktime()
|
||||
picker.list:set_target()
|
||||
picker.input:set("", "")
|
||||
picker:find()
|
||||
end, { cwd = picker:cwd() })
|
||||
end, { cwd = picker:cwd() })
|
||||
end)
|
||||
end
|
||||
|
||||
function M.git_branch_del(picker, item)
|
||||
if not (item and item.branch) then
|
||||
Snacks.notify.warn("No branch or commit found", { title = "Snacks Picker" })
|
||||
end
|
||||
|
||||
local branch = item.branch
|
||||
Snacks.picker.util.cmd({ "git", "rev-parse", "--abbrev-ref", "HEAD" }, function(data)
|
||||
-- Check if we are on the same branch
|
||||
if data[1]:match(branch) ~= nil then
|
||||
Snacks.notify.error("Cannot delete the current branch.", { title = "Snacks Picker" })
|
||||
return
|
||||
end
|
||||
|
||||
-- Proceed with deletion
|
||||
Snacks.picker.util.cmd({ "git", "branch", "-d", branch }, function()
|
||||
Snacks.notify("Deleted Branch `" .. branch .. "`", { title = "Snacks Picker" })
|
||||
vim.cmd.checktime()
|
||||
picker.list:set_selected()
|
||||
picker.list:set_target()
|
||||
picker:find()
|
||||
end, { cwd = picker:cwd() })
|
||||
end, { cwd = picker:cwd() })
|
||||
end
|
||||
|
||||
---@param items snacks.picker.Item[]
|
||||
---@param opts? {win?:number}
|
||||
local function setqflist(items, opts)
|
||||
|
|
|
@ -191,6 +191,14 @@ M.git_branches = {
|
|||
format = "git_branch",
|
||||
preview = "git_log",
|
||||
confirm = "git_checkout",
|
||||
win = {
|
||||
input = {
|
||||
keys = {
|
||||
["<c-a>"] = { "git_branch_add", mode = { "n", "i" } },
|
||||
["<c-x>"] = { "git_branch_del", mode = { "n", "i" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
on_show = function(picker)
|
||||
for i, item in ipairs(picker:items()) do
|
||||
if item.current then
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue