Skip to content

Commit 0bc0a58

Browse files
authored
Merge pull request #1658 from actionshrimp/refs-view-branch-delete
Refs view: branch deletion
2 parents d588510 + 3df7669 commit 0bc0a58

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

Diff for: lua/neogit/buffers/refs_view/init.lua

+39
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local config = require("neogit.config")
33
local ui = require("neogit.buffers.refs_view.ui")
44
local popups = require("neogit.popups")
55
local status_maps = require("neogit.config").get_reversed_status_maps()
6+
local mapping = config.get_reversed_refs_view_maps()
67
local CommitViewBuffer = require("neogit.buffers.commit_view")
78
local Watcher = require("neogit.watcher")
89
local logger = require("neogit.logger")
@@ -49,6 +50,36 @@ function M.is_open()
4950
return (M.instance and M.instance.buffer and M.instance.buffer:is_visible()) == true
5051
end
5152

53+
function M._do_delete(ref)
54+
if not ref.remote then
55+
git.branch.delete(ref.unambiguous_name)
56+
else
57+
git.cli.push.remote(ref.remote).delete.to(ref.name).call()
58+
end
59+
end
60+
61+
function M.delete_branch(ref)
62+
if ref then
63+
local input = require("neogit.lib.input")
64+
local message = ("Delete branch: '%s'?"):format(ref.unambiguous_name)
65+
if input.get_permission(message) then
66+
M._do_delete(ref)
67+
end
68+
end
69+
end
70+
71+
function M.delete_branches(refs)
72+
if #refs > 0 then
73+
local input = require("neogit.lib.input")
74+
local message = ("Delete %s branch(es)?"):format(#refs)
75+
if input.get_permission(message) then
76+
for _, ref in ipairs(refs) do
77+
M._do_delete(ref)
78+
end
79+
end
80+
end
81+
end
82+
5283
--- Opens the RefsViewBuffer
5384
function M:open()
5485
if M.is_open() then
@@ -108,6 +139,10 @@ function M:open()
108139
item = { name = items },
109140
}
110141
end),
142+
[mapping["DeleteBranch"]] = function()
143+
M.delete_branches(self.buffer.ui:get_refs_under_cursor())
144+
self:redraw()
145+
end,
111146
},
112147
n = {
113148
[popups.mapping_for("CherryPickPopup")] = popups.open("cherry_pick", function(p)
@@ -121,6 +156,10 @@ function M:open()
121156
suggested_branch_name = ref and ref.name,
122157
}
123158
end),
159+
[mapping["DeleteBranch"]] = function()
160+
M.delete_branch(self.buffer.ui:get_ref_under_cursor())
161+
self:redraw()
162+
end,
124163
[popups.mapping_for("CommitPopup")] = popups.open("commit", function(p)
125164
p { commit = self.buffer.ui:get_commits_in_selection()[1] }
126165
end),

Diff for: lua/neogit/config.lua

+16-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ end
6161
function M.get_reversed_commit_editor_maps_I()
6262
return get_reversed_maps("commit_editor_I")
6363
end
64+
---
65+
---@return table<string, string[]>
66+
function M.get_reversed_refs_view_maps()
67+
return get_reversed_maps("refs_view")
68+
end
6469

6570
---@param set string
6671
---@return table<string, string[]>
@@ -278,6 +283,11 @@ end
278283
---| "Abort"
279284
---| false
280285
---| fun()
286+
---
287+
---@alias NeogitConfigMappingsRefsView
288+
---| "DeleteBranch"
289+
---| false
290+
---| fun()
281291

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

304315
---@class NeogitConfig Neogit configuration settings
305316
---@field filewatcher? NeogitFilewatcherConfig Values for filewatcher
@@ -583,6 +594,9 @@ function M.get_default_values()
583594
["<LeftMouse>"] = "MouseClick",
584595
["<2-LeftMouse>"] = "NOP",
585596
},
597+
refs_view = {
598+
["x"] = "DeleteBranch",
599+
},
586600
popup = {
587601
["?"] = "HelpPopup",
588602
["A"] = "CherryPickPopup",
@@ -1211,7 +1225,8 @@ function M.setup(opts)
12111225
end
12121226

12131227
if opts.use_default_keymaps == false then
1214-
M.values.mappings = { status = {}, popup = {}, finder = {}, commit_editor = {}, rebase_editor = {} }
1228+
M.values.mappings =
1229+
{ status = {}, popup = {}, finder = {}, commit_editor = {}, rebase_editor = {}, refs_view = {} }
12151230
else
12161231
-- Clear our any "false" user mappings from defaults
12171232
for section, maps in pairs(opts.mappings or {}) do

Diff for: lua/neogit/lib/ui/init.lua

+20
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,26 @@ function Ui:get_ref_under_cursor()
349349

350350
return component and component.options.ref
351351
end
352+
---
353+
---@return ParsedRef[]
354+
function Ui:get_refs_under_cursor()
355+
local range = { vim.fn.getpos("v")[2], vim.fn.getpos(".")[2] }
356+
table.sort(range)
357+
local start, stop = unpack(range)
358+
359+
local refs = {}
360+
for i = start, stop do
361+
local component = self:_find_component_by_index(i, function(node)
362+
return node.options.ref ~= nil
363+
end)
364+
365+
if component then
366+
table.insert(refs, 1, component.options.ref)
367+
end
368+
end
369+
370+
return util.deduplicate(refs)
371+
end
352372

353373
---@return string|nil
354374
function Ui:get_yankable_under_cursor()

0 commit comments

Comments
 (0)