feat(picker.git): add create and delete branch to git_branches (#909)
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:
Ian Liu 2025-02-05 11:01:44 -08:00 committed by GitHub
parent 6069a1e4df
commit 8676c409e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 63 additions and 0 deletions

View file

@ -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)