Skip to content

Refs view: branch deletion #1658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lua/neogit/buffers/refs_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local config = require("neogit.config")
local ui = require("neogit.buffers.refs_view.ui")
local popups = require("neogit.popups")
local status_maps = require("neogit.config").get_reversed_status_maps()
local mapping = config.get_reversed_refs_view_maps()
local CommitViewBuffer = require("neogit.buffers.commit_view")
local Watcher = require("neogit.watcher")
local logger = require("neogit.logger")
Expand Down Expand Up @@ -49,6 +50,36 @@ function M.is_open()
return (M.instance and M.instance.buffer and M.instance.buffer:is_visible()) == true
end

function M._do_delete(ref)
if not ref.remote then
git.branch.delete(ref.unambiguous_name)
else
git.cli.push.remote(ref.remote).delete.to(ref.name).call()
end
end

function M.delete_branch(ref)
if ref then
local input = require("neogit.lib.input")
local message = ("Delete branch: '%s'?"):format(ref.unambiguous_name)
if input.get_permission(message) then
M._do_delete(ref)
end
end
end

function M.delete_branches(refs)
if #refs > 0 then
local input = require("neogit.lib.input")
local message = ("Delete %s branch(es)?"):format(#refs)
if input.get_permission(message) then
for _, ref in ipairs(refs) do
M._do_delete(ref)
end
end
end
end

--- Opens the RefsViewBuffer
function M:open()
if M.is_open() then
Expand Down Expand Up @@ -108,6 +139,10 @@ function M:open()
item = { name = items },
}
end),
[mapping["DeleteBranch"]] = function()
M.delete_branches(self.buffer.ui:get_refs_under_cursor())
self:redraw()
end,
},
n = {
[popups.mapping_for("CherryPickPopup")] = popups.open("cherry_pick", function(p)
Expand All @@ -121,6 +156,10 @@ function M:open()
suggested_branch_name = ref and ref.name,
}
end),
[mapping["DeleteBranch"]] = function()
M.delete_branch(self.buffer.ui:get_ref_under_cursor())
self:redraw()
end,
[popups.mapping_for("CommitPopup")] = popups.open("commit", function(p)
p { commit = self.buffer.ui:get_commits_in_selection()[1] }
end),
Expand Down
17 changes: 16 additions & 1 deletion lua/neogit/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ end
function M.get_reversed_commit_editor_maps_I()
return get_reversed_maps("commit_editor_I")
end
---
---@return table<string, string[]>
function M.get_reversed_refs_view_maps()
return get_reversed_maps("refs_view")
end

---@param set string
---@return table<string, string[]>
Expand Down Expand Up @@ -278,6 +283,11 @@ end
---| "Abort"
---| false
---| fun()
---
---@alias NeogitConfigMappingsRefsView
---| "DeleteBranch"
---| false
---| fun()

---@alias NeogitGraphStyle
---| "ascii"
Expand All @@ -300,6 +310,7 @@ end
---@field rebase_editor_I? { [string]: NeogitConfigMappingsRebaseEditor_I } A dictionary that uses Rebase editor commands to set a single keybind
---@field commit_editor? { [string]: NeogitConfigMappingsCommitEditor } A dictionary that uses Commit editor commands to set a single keybind
---@field commit_editor_I? { [string]: NeogitConfigMappingsCommitEditor_I } A dictionary that uses Commit editor commands to set a single keybind
---@field refs_view? { [string]: NeogitConfigMappingsRefsView } A dictionary that uses Refs view editor commands to set a single keybind

---@class NeogitConfig Neogit configuration settings
---@field filewatcher? NeogitFilewatcherConfig Values for filewatcher
Expand Down Expand Up @@ -583,6 +594,9 @@ function M.get_default_values()
["<LeftMouse>"] = "MouseClick",
["<2-LeftMouse>"] = "NOP",
},
refs_view = {
["x"] = "DeleteBranch",
},
popup = {
["?"] = "HelpPopup",
["A"] = "CherryPickPopup",
Expand Down Expand Up @@ -1211,7 +1225,8 @@ function M.setup(opts)
end

if opts.use_default_keymaps == false then
M.values.mappings = { status = {}, popup = {}, finder = {}, commit_editor = {}, rebase_editor = {} }
M.values.mappings =
{ status = {}, popup = {}, finder = {}, commit_editor = {}, rebase_editor = {}, refs_view = {} }
else
-- Clear our any "false" user mappings from defaults
for section, maps in pairs(opts.mappings or {}) do
Expand Down
20 changes: 20 additions & 0 deletions lua/neogit/lib/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,26 @@ function Ui:get_ref_under_cursor()

return component and component.options.ref
end
---
---@return ParsedRef[]
function Ui:get_refs_under_cursor()
local range = { vim.fn.getpos("v")[2], vim.fn.getpos(".")[2] }
table.sort(range)
local start, stop = unpack(range)

local refs = {}
for i = start, stop do
local component = self:_find_component_by_index(i, function(node)
return node.options.ref ~= nil
end)

if component then
table.insert(refs, 1, component.options.ref)
end
end

return util.deduplicate(refs)
end

---@return string|nil
function Ui:get_yankable_under_cursor()
Expand Down
Loading